Skip to content

Getting Started

Terminal window
pip install pybenchx
# or
uv pip install pybenchx

Create examples/hello_bench.py:

from pybench import bench
@bench(name="hello", n=1_000, repeat=10)
def hello():
return sum(range(50))

Run:

Terminal window
pybench examples/

Filter and tweak at runtime:

Terminal window
pybench examples/ -k hello -P repeat=5 -P n=10_000
from pybench import Bench, BenchContext
suite = Bench("math")
@suite.bench(name="baseline", baseline=True, repeat=10)
def baseline(b: BenchContext):
setup = list(range(100))
b.start()
sum(setup) # timed
b.end()

Why: context mode excludes per-iteration setup from timing, improving signal.

  • Directories expand to **/*bench.py.
  • Name cases with name=. Use group= to cluster related cases.
@suite.bench(name="join-basic", group="strings")
  • Set baseline=True on one case per group. Others show “vs base”.
  • Without an explicit baseline, a case whose name includes “baseline/base” may be used.
Terminal window
pybench examples/ --profile fast # ~150ms per variant, repeat=10
pybench examples/ --profile thorough # ~1s per variant, repeat=30
pybench examples/ --profile smoke # no calibration, repeat=3
  • Read CLI for options (--no-color, --sort, --budget, --max-n).
  • See API for parameterization (params={...}) and suites.
  • Explore Examples for common patterns.