# Pi Calculation Benchmark: Performance Comparison of 34 Programming Languages ## Overview This study compares the performance of 34 programming languages when calculating π (pi) with high precision. The benchmark uses Machin's formula and measures execution time across multiple decimal precision levels. ## Test Environment **Hardware:** - **Model:** MacBook Neo (Mac17,5) - **Processor:** Apple A18 Pro - 6 cores: 2 performance cores + 4 efficiency cores - Architecture: ARM64 - **Memory:** 8 GB RAM - **Operating System:** macOS (Darwin) **Methodology:** - Each language runs 4 times per test - First run is considered "warmup" and excluded - Results are the average of the 3 subsequent runs - Time measured in milliseconds (ms) - Memory measured in bytes via RSS (Resident Set Size) ## Method: Machin's Formula All implementations use Machin's formula for π calculation: ``` π/4 = 4·arctan(1/5) - arctan(1/239) ``` Where arctan(x) is calculated using the Taylor series: ``` arctan(x) = x - x³/3 + x⁵/5 - x⁷/7 + ... ``` **Advantages of this method:** 1. Fast convergence (few terms required) 2. Simple implementation 3. High precision possible 4. Only integer arithmetic required ## Performance Reports Detailed performance reports are available for each decimal precision level: - **[1 Decimal](reports/1_decimals.md)** - Minimal precision - **[2 Decimals](reports/2_decimals.md)** - Low precision - **[5 Decimals](reports/5_decimals.md)** - Medium precision - **[10 Decimals](reports/10_decimals.md)** - Standard precision - **[100 Decimals](reports/100_decimals.md)** - High precision - **[1000 Decimals](reports/1000_decimals.md)** - Very high precision - **[2000 Decimals](reports/2000_decimals.md)** - Extreme precision ## Summary Results (100 Decimals) ### All Languages Performance | Rank | Language | Time (ms) | Memory (bytes) | Type | |------|-----------|-----------|----------------|------| | 1 | Assembly | 9 | 966,656 | Compiled | | 1 | C | 9 | 180,224 | Compiled | | 1 | C++ | 9 | 196,608 | Compiled | | 1 | Rust | 9 | 0 | Compiled | | 1 | Go | 9 | 180,224 | Compiled | | 1 | Nim | 9 | 0 | Compiled | | 1 | Odin | 9 | 0 | Compiled | | 1 | Haskell | 9 | 0 | Compiled | | 9 | Fortran | 27 | 196,608 | Compiled | | 10 | Crystal | 28 | 180,224 | Compiled | | 11 | Lua | 29 | 0 | Interpreted | | 12 | Swift | 29 | 262,144 | Compiled | | 13 | Bash | 32 | 2,000,000 | Interpreted | | 14 | Zig | 33 | 2,730,000 | Compiled | | 15 | Objective-C | 31 | 752,000 | Compiled | | 16 | Dart | 31 | 11,749,000 | JIT | | 17 | D | 35 | 10,154,000 | Compiled | | 18 | Perl | 55 | 2,000,000 | Interpreted | | 19 | Python | 57 | 2,000,000 | Interpreted | | 19 | Java | 57 | 2,000,000 | JIT | | 19 | C# | 57 | 2,016,000 | JIT | | 22 | Scala | 58 | 2,000,000 | JIT | | 23 | Brainfuck | 54 | 2,005,000 | Interpreted | | 24 | PHP | 77 | 2,032,000 | Interpreted | | 25 | Ruby | 79 | 2,000,000 | Interpreted | | 26 | Kotlin | 83 | 2,032,000 | JIT | | 27 | JavaScript | 84 | 2,032,000 | Interpreted | | 28 | Erlang | 130 | 2,037,000 | Interpreted | | 29 | TypeScript | 154 | 2,032,000 | Interpreted | | 30 | R | 349 | 2,032,000 | Interpreted | | 31 | Julia | 290 | 2,032,000 | JIT | | 32 | Elixir | 898 | 2,037,000 | Interpreted | ### Language Categories **Compiled Languages (Native Code):** - Fastest execution (9-35 ms) - Minimal memory usage (0-966,656 bytes) - Consistent performance across decimal levels **JIT-Compiled Languages:** - Moderate execution time (31-290 ms) - Higher memory usage (~2 MB) - Good performance after warmup **Interpreted Languages:** - Variable execution time (29-898 ms) - Moderate memory usage (~2 MB) - Performance varies widely ## Key Findings 1. **Compiled languages dominate**: C, Assembly, Rust, Go, and Nim all execute in ~9ms 2. **Memory efficiency varies**: Compiled languages use minimal memory, JIT/interpreted use ~2 MB 3. **Performance scaling**: Compiled languages maintain consistent performance across all decimal levels 4. **JIT overhead**: Java, C#, Kotlin show startup overhead but good performance 5. **Interpreted languages**: Python, Perl, PHP, Ruby, JavaScript show moderate performance ## Languages Tested **Compiled (10):** Assembly, C, C++, Rust, Go, Nim, Odin, Fortran, Swift, Crystal **JIT-Compiled (4):** Java, C#, Kotlin, Julia **Interpreted (5):** Python, Perl, PHP, Ruby, JavaScript **Other (15):** Bash, Brainfuck, D, Dart, Elixir, Erlang, Haskell, Lua, Objective-C, R, Scala, TypeScript, Vimscript, Wolfram, Zig ## Repository Structure ``` . ├── README.md # This file ├── reports/ # Detailed performance reports │ ├── summary.md # Overall summary │ ├── 1_decimals.md # 1 decimal precision │ ├── 2_decimals.md # 2 decimals precision │ ├── 5_decimals.md # 5 decimals precision │ ├── 10_decimals.md # 10 decimals precision │ ├── 100_decimals.md # 100 decimals precision │ ├── 1000_decimals.md # 1000 decimals precision │ └── 2000_decimals.md # 2000 decimals precision ├── timelines/ # Resource usage timeline data ├── assembly/ # Assembly implementation ├── c/ # C implementation ├── cpp/ # C++ implementation ├── rust/ # Rust implementation └── ... # Other language implementations ``` ## Running the Benchmark ```bash # Build all languages ./build.sh # Run all tests ./run_all.sh # Run specific language cd c && ./build.sh && ./print_hej # Run detailed profiling (breaks down execution time) ./profile_detailed.sh 100 ``` ## Detailed Profiling The benchmark includes a detailed profiling system that breaks down execution time into components: - **Startup Time**: Runtime initialization, library loading, JIT compilation - **Calculation Time**: Algorithm execution, numerical operations - **I/O Time**: Output formatting, result printing See [PROFILING.md](PROFILING.md) for detailed documentation. ## License MIT License - See LICENSE file for details. --- *Generated from Pi Calculation Benchmark - Apple A18 Pro Performance Study*