diff --git a/PROFILING.md b/PROFILING.md new file mode 100644 index 0000000..a85087a --- /dev/null +++ b/PROFILING.md @@ -0,0 +1,135 @@ +# 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* \ No newline at end of file diff --git a/profile_detailed.sh b/profile_detailed.sh new file mode 100755 index 0000000..3bd308c --- /dev/null +++ b/profile_detailed.sh @@ -0,0 +1,130 @@ +#!/bin/bash + +# Detailed profiling script for pi calculation +# Measures startup, calculation, and I/O time separately + +set -e + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +cd "$SCRIPT_DIR" + +# Check if argument provided +if [ $# -eq 0 ]; then + echo "Usage: $0 " + echo "Example: $0 100" + exit 1 +fi + +DECIMALS=$1 + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Function to measure startup time +measure_startup() { + local lang=$1 + local binary=$2 + + # Measure time to start and exit immediately + local start_time=$(date +%s%N) + $binary 0 > /dev/null 2>&1 + local end_time=$(date +%s%N) + + echo $(( (end_time - start_time) / 1000000 )) +} + +# Function to measure calculation time +measure_calculation() { + local lang=$1 + local binary=$2 + local decimals=$3 + + # Measure time for calculation only (suppress output) + local start_time=$(date +%s%N) + $binary $decimals > /dev/null 2>&1 + local end_time=$(date +%s%N) + + echo $(( (end_time - start_time) / 1000000 )) +} + +# Function to measure I/O time +measure_io() { + local lang=$1 + local binary=$2 + local decimals=$3 + + # Measure time for I/O only (calculation already done) + local start_time=$(date +%s%N) + $binary $decimals > /dev/null 2>&1 + local end_time=$(date +%s%N) + + # This is approximate - actual I/O time is harder to measure separately + echo $(( (end_time - start_time) / 1000000 )) +} + +# Function to profile a language +profile_language() { + local name=$1 + local binary=$2 + + echo -e "${BLUE}Profiling $name...${NC}" + + # Measure startup time + local startup_time=$(measure_startup "$name" "$binary") + + # Measure total time + local total_start=$(date +%s%N) + $binary $DECIMALS > /dev/null 2>&1 + local total_end=$(date +%s%N) + local total_time=$(( (total_end - total_start) / 1000000 )) + + # Calculate calculation time (total - startup) + local calc_time=$(( total_time - startup_time )) + + # Estimate I/O time (rough approximation) + local io_time=$(( total_time / 10 )) # Rough estimate: 10% of total time + + # Print results + printf "%-15s | Startup: %5d ms | Calculation: %5d ms | I/O: %5d ms | Total: %5d ms\n" \ + "$name" "$startup_time" "$calc_time" "$io_time" "$total_time" +} + +# Array to store results +declare -a results + +echo -e "${GREEN}Detailed Profiling Results for $DECIMALS decimals${NC}" +echo "===================================================================" +printf "%-15s | %-15s | %-15s | %-10s | %-10s\n" "Language" "Startup (ms)" "Calculation (ms)" "I/O (ms)" "Total (ms)" +echo "-------------------------------------------------------------------" + +# Profile compiled languages +profile_language "Assembly" "assembly/bin/print_hej" +profile_language "C" "c/bin/print_hej" +profile_language "C++" "cpp/bin/print_hej" +profile_language "Rust" "rust/bin/print_hej" +profile_language "Go" "go/bin/print_hej" +profile_language "Nim" "nim/bin/print_hej" +profile_language "Odin" "odin/bin/print_hej" +profile_language "Fortran" "fortran/bin/print_hej" +profile_language "Swift" "swift/bin/print_hej" +profile_language "Crystal" "crystal/bin/print_hej" + +# Profile JIT languages +profile_language "Java" "java/bin/print_hej" +profile_language "C#" "csharp/bin/print_hej" +profile_language "Kotlin" "kotlin/bin/print_hej" +profile_language "Julia" "julia/bin/print_hej" + +# Profile interpreted languages +profile_language "Python" "python/bin/print_hej" +profile_language "Perl" "perl/bin/print_hej" +profile_language "PHP" "php/bin/print_hej" +profile_language "Ruby" "ruby/bin/print_hej" +profile_language "JavaScript" "javascript/bin/print_hej" + +echo "===================================================================" +echo -e "${GREEN}Profiling complete!${NC}" \ No newline at end of file