Skip to main content

larzbench

Ergonomic micro-benchmarking. Pure Python, zero dependencies.

timeit works, but it's awkward - stringy, one number. larzbench measures a callable across several rounds, auto-calibrates the iteration count, and gives you real statistics plus a one-call compare() that ranks several implementations.

from larzbench import benchmark, compare

r = benchmark(lambda: sum(range(1000)))
print(r)      # <lambda>: best 8.10us  mean 8.44us  118,000 ops/s

compare({
    "listcomp": lambda: [x*x for x in range(1000)],
    "map":      lambda: list(map(lambda x: x*x, range(1000))),
}).report()
# listcomp   42.10us/op  23,753 ops/s  (fastest)
# map        61.30us/op  16,313 ops/s  1.46x slower

Why

  • Real stats, not one number. best / mean / median / stdev and ops-per-second, over multiple rounds with a warmup - so noise and cold caches don't fool you.
  • Auto-calibration. It picks an iteration count that runs long enough to be meaningful, so you don't guess.
  • compare() is the killer feature. Pass a dict of implementations; get them ranked fastest-first with relative slowdowns and a printable report.
  • Testable. Pass a clock to make timings deterministic (this repo's tests do exactly that).
  • Zero dependencies.

Install

pip install larzbench

Usage

from larzbench import benchmark, compare

benchmark(func, *args, rounds=5, iterations=None, warmup=True, name=None, **kwargs)
# -> Result: .best .mean .median .stdev .ops_per_sec .per_op

compare({"a": fn_a, "b": fn_b}, rounds=7).report()

Tests

python -m unittest discover -s tests -v   # 7 tests (deterministic via a fake clock)

The Larz stack

One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.

License

MIT (c) larz-scripter

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

larzbench-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

larzbench-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file larzbench-0.1.0.tar.gz.

File metadata

  • Download URL: larzbench-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzbench-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b88ced150658d49e9b63fcb00b78e410f70242d7a332b79bfc6d689aef29c468
MD5 07d5c71e3175ee63e8f9b097bb33fbca
BLAKE2b-256 37886676165d72d66e258f817a0dda714eb4311eb7ffe88a767229625812ac7e

See more details on using hashes here.

File details

Details for the file larzbench-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzbench-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzbench-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd2685a9fa8c156777bce09ff9d8ef326680b68696af82bd1da8134958682be6
MD5 785fb23aeb8f810e30150b83daf9a504
BLAKE2b-256 ccc08b1fa561b60cda2912c60ef89b51e5d5bc7ff442476e3481256051c14b25

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page