Skip to content

Measure performance

The benchmark calls each implementation with the same integer, verifies its answer against Python, and prints the median nanoseconds per call and speedup relative to Python. It also reports the interpreter, platform, and sample sizes.

make benchmark
uv run --locked python -m comparison.benchmark -n 0 --calls 200000 --repeat 7
uv run --locked python -m comparison.benchmark -n 44 --calls 200000 --repeat 7

To generate a Markdown table with the same settings as the README, run this command from the repository root after installing the toolchain. Each speedup is the Python median divided by the corresponding implementation's median.

uv run --locked python -m comparison.benchmark --markdown -n 20 --calls 1000000 --repeat 9

At index 0 the native loop has no iterations, so the measurement is dominated by calling the function, converting arguments and results, and checking the range. At index 44 the same call includes 44 additions. The benchmark looks up the callable before timing and keeps imports, compilation, and printing outside the timed region.

The defaults are index 20, 100,000 calls per sample, and five samples. Run on an idle machine and record the git revision, Python version, compiler versions, operating system, and processor when sharing results. The original Python 3.6 speedups describe the environment used for that workshop. Timing tiny scalar calls here tells you little about the cost of passing a large array or calling back into Python for each element.