Skip to main content

PyBencher is a Python benchmarking module for benchmarking several python functions at once. PyBencher supports tuneable benchmarking parameters as well as args and kwargs for function calls.

Project description

PyBencher

GitHub Release PyPI Downloads GitHub Actions Workflow Status

PyBencher is a simple, decorator-based benchmarking suite for Python. It provides detailed timing statistics (average, median, standard deviation) and supports per-test configuration overrides.

Installation

pip install pybencher

Basic Usage

from pybencher import Suite

suite = Suite()

# Quick registration
@suite.bench()
def my_function():
    return sum(range(10000))

# Custom configuration using 'bench_' prefix
@suite.bench(bench_name="Fast Math", bench_max_itr=5000)
def fast():
    return 1 + 1

# Positional and keyword arguments are passed directly
@suite.bench(10, 20, bench_name="Add")
def add(a, b):
    return a + b

# Run and print results
results = suite.run()
results.print()

# Manual registration (equivalent to @suite.bench)
def manual_func(n):
    return sum(range(n))

suite.add(manual_func, 1000, bench_name="Manual Register")

Configuration Overrides

Any setting in the Suite can be overridden for a specific benchmark by prefixing it with bench_. This ensures that benchmark configuration does not interfere with your function's own parameters (e.g., using timeout=0.5 as a function argument while setting bench_timeout=5.0 for the suite).

Override Type Description
bench_name str Display name in reports
bench_timeout float Per-test time limit in seconds
bench_max_itr int Maximum execution count
bench_min_itr int Minimum execution count
bench_cut float Percentage of outliers to trim (0.0 to 0.5)
bench_disable_stdout bool Mute print() output inside the target
bench_verbose bool Include extra stats in results.print()
bench_before callable Local setup hook
bench_after callable Local teardown hook

Reference

Suite

  • timeout (float): Default time limit (10s).
  • max_itr (int): Default max runs (1000).
  • min_itr (int): Default min runs (3).
  • cut (float): Default outlier threshold (0.05).
  • disable_stdout (bool): Global stdout suppressor.
  • verbose (bool): Global verbosity flag.

BenchmarkResults

  • print(verbose=None): Print results to console.
  • to_json(indent=4): Export results to JSON.
  • to_list(): Export results to list of dicts.

BenchmarkResult

Dataclass containing:

  • name: Target name or custom override.
  • avg, std, median, minimum, maximum: Timing stats in seconds.
  • itr_ps: Iterations per second.
  • iterations: Total runs.
  • counted_iterations: Runs used for stats after trimming outliers.

Example

from pybencher import Suite

suite = Suite()

# 'timeout' here is a function param, not the benchmark limit
@suite.bench(timeout=0.1, bench_name="Sleepy", bench_verbose=True)
def test_args(timeout):
    import time
    time.sleep(timeout)

results = suite.run()
results.print()

Output:

Sleepy: 100ms/itr | 10.0 itr/s
  std:     120us
  median:  100ms
  min/max: 99ms / 101ms
  runs:    10 (10 counted)
  total:   1.01s

Project details


Download files

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

Source Distribution

pybencher-2.0.0.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

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

pybencher-2.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pybencher-2.0.0.tar.gz.

File metadata

  • Download URL: pybencher-2.0.0.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pybencher-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3687eb06cd1299637c0adaae5941752c1b9362557e6aa6d53449733213e39f35
MD5 d218ae4ec33cc2574e157e2aae4111e2
BLAKE2b-256 d52269915f1509d60480642f01456597caf4488a4eb050a2346642f234268862

See more details on using hashes here.

File details

Details for the file pybencher-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pybencher-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pybencher-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd41c7716a2938c7dafe9c34ac35d210792169d3d091f5fac82337eee14836d6
MD5 02b9a4996fd3cdf3a960c9a5e0097146
BLAKE2b-256 cc1d2453c290059b8376ec2791f8b07fa79ee7f4a9745dc76da9cab5021fec54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page