# Detailed Profiling System

This document describes the detailed profiling system that breaks down execution time into components.

## Overview

The detailed profiling system measures:

1. **Startup Time** - Time to initialize runtime and load libraries
2. **Calculation Time** - Time for the actual π calculation
3. **I/O Time** - Time for output formatting and printing
4. **Memory Allocation** - Memory usage during execution

## Usage

### Basic Profiling

```bash
# Profile all languages with 100 decimals
./profile_detailed.sh 100

# Profile with 1000 decimals
./profile_detailed.sh 1000

# Profile with 20000 decimals
./profile_detailed.sh 20000
```

### Output Format

```
Language        | Startup (ms)    | Calculation (ms) | I/O (ms)   | Total (ms)
-------------------------------------------------------------------
Assembly        |     2           |       7          |     0      |     9
C               |     1           |       8          |     0      |     9
Python          |    15           |      40          |     2      |    57
Java            |    30           |      25          |     2      |    57
```

## Time Components

### Startup Time

**What it measures:**
- Runtime initialization
- Library loading
- JIT compilation (for JIT languages)
- Memory allocation for runtime

**Typical values:**
- **Compiled languages**: 1-5 ms (minimal runtime)
- **JIT languages**: 20-50 ms (JIT compilation overhead)
- **Interpreted languages**: 10-30 ms (interpreter startup)

### Calculation Time

**What it measures:**
- Algorithm execution
- Numerical operations
- Memory operations for calculation
- Loop iterations

**Typical values:**
- **Compiled languages**: 5-30 ms (optimized machine code)
- **JIT languages**: 20-400 ms (depends on JIT optimization)
- **Interpreted languages**: 30-900 ms (interpreted execution)

### I/O Time

**What it measures:**
- String formatting
- Output buffering
- Console/terminal output
- Result formatting

**Typical values:**
- **All languages**: 0-5 ms (minimal for most languages)

## Implementation Details

### How It Works

1. **Startup Measurement**: Runs the program with 0 decimals and measures time
2. **Total Measurement**: Runs the program with specified decimals
3. **Calculation Estimation**: Subtracts startup from total time
4. **I/O Estimation**: Approximates I/O time (rough estimate)

### Limitations

- **I/O time is approximate**: Difficult to measure separately
- **Startup time varies**: First run may be slower due to caching
- **Memory measurement**: Not yet implemented in detail

## Future Enhancements

1. **Memory Profiling**: Track memory allocation over time
2. **CPU Profiling**: Measure CPU usage during execution
3. **Cache Analysis**: Analyze cache hit/miss rates
4. **Detailed Breakdown**: Separate initialization from calculation

## Example Results

### 100 Decimals

| Language | Startup | Calculation | I/O | Total |
|----------|---------|-------------|-----|-------|
| C | 1 ms | 8 ms | 0 ms | 9 ms |
| Python | 15 ms | 40 ms | 2 ms | 57 ms |
| Java | 30 ms | 25 ms | 2 ms | 57 ms |

### 1000 Decimals

| Language | Startup | Calculation | I/O | Total |
|----------|---------|-------------|-----|-------|
| C | 1 ms | 29 ms | 0 ms | 30 ms |
| Python | 15 ms | 18 ms | 0 ms | 33 ms |
| Java | 30 ms | 46 ms | 0 ms | 76 ms |

## Integration with Main Benchmark

The detailed profiling can be run alongside the main benchmark:

```bash
# Run main benchmark
./run_all.sh 100

# Run detailed profiling
./profile_detailed.sh 100
```

Results are stored in `timelines/` directory for further analysis.

---

*Generated from Pi Calculation Benchmark - Detailed Profiling System*