A comparative benchmark of N-body gravitational simulation implemented in Python, Go, and Mojo to evaluate their performance characteristics for computationally intensive floating-point operations.
The benchmark results show significant performance differences between the three implementations:
- Python: ~179 seconds (baseline)
- Go: ~7.8 seconds (~23x faster than Python)
- Mojo: ~7.2 seconds (~25x faster than Python)
These results were obtained by running the simulation with 2000 bodies for 1000 iterations on the same hardware.
This project implements the same N-body simulation algorithm in three different languages and provides a benchmarking framework to compare their performance. The simulation models gravitational interactions between celestial bodies using Newtonian physics.
The simulation uses a direct N² pairwise force calculation approach based on Newton's law of universal gravitation. For each pair of bodies, the gravitational force is calculated and applied according to:
F = G (m₁ m₂) / r²
Where:
- G is the gravitational constant (6.67430 × 10⁻¹¹)
- m₁ and m₂ are the masses of the two bodies
- r is the distance between the bodies
The simulation follows these key physics principles:
For each pair of bodies (i and j), we calculate the vector from body i to body j:
r_vec = position_j - position_i
This vector points from body i toward body j and is essential for determining the direction of the gravitational force.
The magnitude of the gravitational force is calculated using Newton's law of universal gravitation:
force_magnitude = G * mass_i * mass_j / (distance * distance)
The gravitational force acts along the line connecting the two bodies. The force vector is calculated as:
force_vector = force_magnitude * (r_vec / |r_vec|)
Where r_vec / |r_vec| is the unit vector pointing from body i to body j.
For each time step (dt):
- Calculate the acceleration: a = F / m
- Update velocity: v = v + a * dt
- Update position: p = p + v * dt
This implements a simple Euler integration method for the equations of motion.
All implementations include these key optimizations:
- Newton's Third Law: Each pair of bodies is computed only once, with equal and opposite forces applied to both bodies
- Energy Conservation: The total energy (kinetic + potential) is calculated to verify simulation accuracy
- Efficient Distance Calculations: Using squared distances where possible to avoid unnecessary square root operations
- Python: Uses NumPy for efficient array operations
- Go: Leverages goroutines for parallel execution
- Mojo: Utilizes SIMD vectorization and parallelization features
PyGoMo/
├── python/ # Python implementation
│ └── nbody.py # N-body simulation in Python with NumPy/Numba
├── go/ # Go implementation
│ └── nbody.go # N-body simulation in Go with goroutines
├── mojo/ # Mojo implementation
│ └── nbody.mojo # N-body simulation in Mojo with SIMD
├── benchmark/ # Benchmarking tools
│ ├── benchmark.py # Script to run and compare implementations
│ └── visualize_results.py # Script to visualize benchmark results
└── run_benchmarks.sh # Shell script to run all benchmarks
- Python 3.7+
- NumPy
- Matplotlib
- Pandas
- Numba (optional, for JIT compilation and parallelization)
- Go 1.16+
- Mojo SDK
To run the benchmark comparison across all implementations:
cd PyGoMo
./run_benchmarks.shThe benchmark will:
- Run each implementation with the specified number of bodies and iterations
- Measure and compare execution times
- Save the results to a CSV file
To visualize the benchmark results:
cd PyGoMo
python benchmark/visualize_results.pyThis will generate a professional visualization of the benchmark results, saved as both PNG and PDF files.
The Python implementation offers two acceleration methods:
- NumPy Vectorization: Uses NumPy broadcasting for efficient array operations
- Numba JIT Compilation: Optional acceleration with Numba's just-in-time compilation and parallelization
Key optimizations:
- Vectorized force calculations using NumPy broadcasting
- Parallel execution with Numba when available
- Optimized memory usage and array operations
The Go implementation uses goroutines to parallelize the force calculations across available CPU cores.
Key optimizations:
- Efficient parallel implementation using goroutines
- Optimized vector operations
- Cache-friendly data structures
- Mutex-free force updates for better performance
The Mojo implementation showcases the language's SIMD and parallelism capabilities.
Key optimizations:
- SIMD-friendly data layout
- Explicit parallelization
- Vectorized operations
- Efficient memory management
Each implementation accepts command-line parameters to adjust:
- Number of bodies
- Number of iterations
- Time step (dt)
- Random seed (for reproducibility)
Example:
cd PyGoMo
python python/nbody.py --bodies 2000 --iterations 1000