This repository contains performance benchmarking implementations of two computationally intensive simulations across three programming languages: Python, Julia, and MATLAB. The benchmarks compare both baseline and optimized versions of each simulation to evaluate language-specific performance characteristics.
Memory-bound simulation that models 2D fluid dynamics using the Lattice Boltzmann Method with a D2Q9 lattice model.
lbm_cylinder.{py,jl,m} (baseline), lbm_cylinder_opt.{py,jl,m} (optimized)Compute-bound simulation that models gravitational interactions between particles in galactic systems.
nbody.{py,jl,m} (baseline), nbody_opt.{py,jl,m} (optimized)Add Julia to your system PATH during installation
VS Code Extension:
Extension ID: julialang.language-julia
Configure Julia in VS Code:
Set the path to your Julia installation (if not auto-detected)
Install Required Packages:
julia
# Open Julia REPL in VS Code (Ctrl/Cmd + Shift + P → "Julia: Start REPL")
using Pkg
Pkg.add(["CairoMakie", "JSON3", "Distributions", "Random", "Statistics"])
Verify installation: open Command Prompt/Terminal and type matlab -batch "version"
VS Code Extension:
MathWorks.language-matlabThis provides syntax highlighting, code navigation, and basic IntelliSense
Configure MATLAB in VS Code:
Example path: C:\Program Files\MATLAB\R2024b\bin\matlab.exe
Running MATLAB Code from VS Code:
bash
# Run MATLAB scripts from VS Code terminal:
matlab -batch "script_name" # Run script (no .m extension)
matlab -batch "function_name()" # Run function
matlab -singleCompThread -batch "script_name" # Single-threaded (for benchmarking)
VS Code Tips for MATLAB:
Ctrl/Cmd + Shift + P → "MATLAB: Open Command Window" to open MATLAB terminal%% to create code sections for interactive executionThe repository includes C and Fortran implementations for additional performance comparison. These require compilation before execution.
Linux/macOS: Install GCC (sudo apt install gcc on Ubuntu, brew install gcc on macOS)
Fortran Compiler:
sudo apt install gfortran on Ubuntu)macOS: Install GFortran (brew install gfortran)
Build Tools:
C Implementations:
# Navigate to C code directory
cd C_lbm/ # or C_nbody/
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake .. -G "MinGW Makefiles" # Windows with MinGW
# or
cmake .. -G "Unix Makefiles" # Linux/macOS
# Compile
cmake --build .
# Run the executable
./lbm_cylinder_c.exe # Windows
./lbm_cylinder_c # Linux/macOS
Fortran Implementations:
# Navigate to Fortran code directory
cd fortran_lbm/ # or fortran_nbody/
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake .. -G "MinGW Makefiles" # Windows with MinGW
# or
cmake .. -G "Unix Makefiles" # Linux/macOS
# Compile
cmake --build .
# Run the executable
./lbm_cylinder_fortran.exe # Windows
./lbm_cylinder_fortran # Linux/macOS
Note: The C and Fortran implementations generate visualization images in their respective images/ folders. These images are included in the repository for reference but are not synced in future commits.
bash
pip install numpy matplotlib numbajson (built-in), pathlib (built-in)julia
using Pkg
Pkg.add([
"CairoMakie", # Plotting and visualization
"JSON3", # JSON file parsing
"Distributions", # Statistical distributions
"Random", # Random number generation
"Statistics", # Statistical functions
"GeometryBasics" # Geometric primitives (for LBM)
])arguments blocks)The benchmark.py script provides automated performance comparison across all three languages with statistical analysis and outlier detection.
Run the benchmark script:
bash
python benchmark.py
Interactive Configuration:
Script Selection: Choose which simulations to benchmark
lbm_cylinder or nbodylbm_cylinder,nbody_optlbm_cylinder,lbm_cylinder_opt,nbody,nbody_optLanguage Selection: Choose which languages to compare
p,j,m (Python, Julia, MATLAB)p,j (Python + Julia only)m (MATLAB only)Benchmark Modes:
# Compare all implementations of LBM cylinder simulation
Script name(s): lbm_cylinder,lbm_cylinder_opt
Language codes: p,j,m
# Compare only optimized versions across Python and Julia
Script name(s): nbody_opt,lbm_cylinder_opt
Language codes: p,j
# Full benchmark of all simulations and languages
Script name(s): lbm_cylinder,lbm_cylinder_opt,nbody,nbody_opt
Language codes: p,j,m
Important: Before running benchmarks, ensure visualization is disabled in all scripts:
VISUALIZE = FalseVISUALIZE = false VISUALIZE = false;This prevents matplotlib/plotting windows from appearing and ensures fair timing measurements.
The script generates:
- Performance rankings with execution times and speedup ratios
- Statistical analysis including standard deviation and outlier detection
- Visualization plots comparing performance across languages
- Detailed reports saved to benchmark_results_* directories
Edit the configuration section in benchmark.py:
N_WARMUPS = 2 # Warmup runs (for JIT compilation)
N_RUNS = 5 # Number of benchmark runs
N_MAX = 10 # Maximum attempts per language
THRESHOLD = 0.20 # Outlier detection threshold (20%)
SCRIPT_TIMEOUT = 900 # Timeout in seconds (15 minutes)
The nbody_ic.py script generates reproducible initial conditions for N-body simulations:
python nbody_ic.py
This creates JSON files (e.g., nbody_ic_galaxy_spiral_N4000.json) that ensure identical starting conditions across all language implementations for fair performance comparison.
python lbm_cylinder.py # LBM baseline
python lbm_cylinder_opt.py # LBM optimized
python nbody.py # N-body baseline
python nbody_opt.py # N-body optimized
julia lbm_cylinder.jl # LBM baseline
julia lbm_cylinder_opt.jl # LBM optimized
julia nbody.jl # N-body baseline
julia nbody_opt.jl # N-body optimized
lbm_cylinder() % LBM baseline
lbm_cylinder_opt() % LBM optimized
nbody() % N-body baseline
nbody_opt() % N-body optimized
For MATLAB benchmarking, run single-threaded:
matlab -singleCompThread -batch "nbody()"
├── README.md # This file
├── benchmark.py # Main benchmarking script
├── nbody_ic.py # Initial conditions generator
│
├── lbm_cylinder.py # LBM Python baseline
├── lbm_cylinder_opt.py # LBM Python optimized
├── lbm_cylinder.jl # LBM Julia baseline
├── lbm_cylinder_opt.jl # LBM Julia optimized
├── lbm_cylinder.m # LBM MATLAB baseline
├── lbm_cylinder_opt.m # LBM MATLAB optimized
│
├── nbody.py # N-body Python baseline
├── nbody_opt.py # N-body Python optimized
├── nbody.jl # N-body Julia baseline
├── nbody_opt.jl # N-body Julia optimized
├── nbody.m # N-body MATLAB baseline
└── nbody_opt.m # N-body MATLAB optimized
The benchmark suite provides insights into: - Absolute performance across languages and optimization levels - Scalability with problem size and complexity - Memory vs. compute bound performance characteristics - JIT compilation effects (Julia, Python/Numba) - Optimization effectiveness across different algorithmic approaches
This benchmarking suite is provided for educational and research purposes. Individual simulation implementations may have their own licensing terms.
$ claude mcp add benchmark_mpj \
-- python -m otcore.mcp_server <graph>