This guide will help you get started with the Python Performance Benchmarking Suite.
- Install the benchmarking dependencies:
cd benchmarks
pip install -r requirements.txtNote: If you encounter issues installing PyTorch, you can skip it and run benchmarks without the PyTorch category.
python benchmarks/run_benchmarks.pyThis will run all benchmark categories and save results to benchmark_results/.
# Run only NumPy benchmarks
python benchmarks/run_benchmarks.py --categories numpy
# Run multiple categories
python benchmarks/run_benchmarks.py --categories numpy memory async# Run a specific test
cd /path/to/repository
python -m pytest benchmarks/numpy/test_array_operations.py::TestArrayCreation --benchmark-only --no-cov -v
# Run all NumPy tests
python -m pytest benchmarks/numpy/ --benchmark-only --no-cov -vAfter running benchmarks, you'll see output like:
-------------------------------------------------------- benchmark: 5 tests -------------------------------------------------------
Name (time in us) Min Max Mean StdDev Median IQR Outliers OPS (Kops/s) Rounds Iterations
------------------------------------------------------------------------------------------------------------------------------
test_benchmark_array_zeros 102.80 554.47 139.68 10.11 137.62 8.87 3420;1587 7.16 40823 1
Key metrics:
- Min/Max: Fastest and slowest execution times
- Mean: Average execution time
- Median: Middle value (less affected by outliers)
- StdDev: Standard deviation (consistency of results)
- OPS: Operations per second (higher is better)
The run_benchmarks.py script automatically generates a summary showing:
- Total benchmarks run
- Category-wise statistics
- Top 5 slowest tests per category
# Run benchmarks with Python 3.11
python3.11 benchmarks/run_benchmarks.py --output-dir results/
# Run benchmarks with Python 3.12
python3.12 benchmarks/run_benchmarks.py --output-dir results/
# Compare the results
python benchmarks/utils/compare_results.py results/benchmarks_py311*.json results/benchmarks_py312*.json# Profile a specific operation
python benchmarks/utils/profile_memory.pypython -m pytest benchmarks/numpy/test_array_operations.py --benchmark-only --no-cov -vpython -m pytest benchmarks/async/test_async_operations.py --benchmark-only --no-cov -vpython -m pytest benchmarks/memory/test_memory_operations.py --benchmark-only --no-cov -vpython -m pytest benchmarks/startup/test_startup_operations.py --benchmark-only --no-cov -v-
Warm-up runs: The benchmark framework automatically performs warm-up runs to ensure fair measurements.
-
Minimize system load: For most accurate results, close other applications and run benchmarks when the system is idle.
-
Multiple runs: Run benchmarks multiple times to account for system variability:
for i in {1..3}; do python benchmarks/run_benchmarks.py --output-dir results/run_$i done
-
Focus on relative changes: When comparing Python versions, focus on relative performance changes rather than absolute values.
If PyTorch is not installed, the PyTorch benchmarks will be skipped automatically. To install PyTorch:
# CPU version
pip install torch --index-url https://download.pytorch.org/whl/cpu
# CUDA version (if you have NVIDIA GPU)
pip install torchIf you see coverage-related errors, make sure to use the --no-cov flag:
python -m pytest benchmarks/ --benchmark-only --no-cov -vSome benchmarks (especially GC and complex operations) can take a long time. You can:
- Skip slow tests:
pytest -m "not slow" - Reduce min-rounds:
pytest --benchmark-min-rounds=3 - Focus on specific categories
- Read the full README.md for detailed information
- Explore benchmark categories
- Learn about comparing Python versions
- Check out the example comparison script