Skip to main content

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))

# Per-benchmark configuration
@suite.bench(name="Fast Math", max_samples=5000)
def fast():
    return 1 + 1

# Function arguments via args / kwargs
@suite.bench(args=(10, 20), name="Add")
def add(a, b):
    return a + b

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

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

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

Configuration Overrides

Any setting in the Suite can be overridden per-benchmark via keyword arguments to bench() or add(). Function inputs are separated cleanly into the args and kwargs parameters.

Override Type Description
name str Display name in reports
timeout float Per-test time limit in seconds
max_samples int Maximum statistical samples to collect
min_samples int Minimum statistical samples to collect
cut float Percentage of outliers to trim (0.0 to 0.5)
disable_stdout bool Mute print() output inside the target
disable_gc bool Disable garbage collection during benchmark
batch_size int Force a specific batch size (0 for auto-calibration)
live_output bool Show real-time progress in the console
verbose bool Include extra stats in results.print()
before callable Local setup hook
after callable Local teardown hook
args tuple Positional arguments for the target function
kwargs dict Keyword arguments for the target function

Reference

Suite

  • timeout (float): Default time limit (10s).
  • max_samples (int): Default max samples (1000).
  • min_samples (int): Default min samples (3).
  • cut (float): Default outlier threshold (0.05).
  • warmup_samples (int): Number of unmeasured runs to perform before benchmarking (0).
  • validate_responses (bool): Enable cross-test output consistency checks (False).
  • validate_limit (int): Max number of iterations to store for full sequence validation (5).
  • disable_stdout (bool): Global stdout suppressor.
  • disable_gc (bool): Global GC control.
  • batch_size (int): Global batch size override.
  • live_output (bool): Global progress toggle.
  • verbose (bool): Global verbosity flag.

BenchmarkResults

  • print(verbose=None): Print results to console.
  • to_json(indent=4): Export results to JSON.
  • to_markdown(): Export results as a GitHub-flavored markdown table.
  • 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 benchmark config override, not a function param
@suite.bench(args=(0.1,), name="Sleepy", verbose=True)
def test_sleep(duration):
    import time
    time.sleep(duration)

# Function kwargs stay separate from benchmark config
@suite.bench(kwargs={"timeout": 0.1}, name="Sleepy Alt", 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

CI/CD Pipeline

PyBencher uses an automated GitHub Actions pipeline:

  • Testing: Every push to any branch triggers a full test suite across Linux, Windows, and macOS for Python 3.10–3.14.
  • Publishing: Pushes to main automatically publish to PyPI if and only if all tests pass.

Release files for pybencher 2.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybencher 2.4.2
File Size Uploaded
pybencher-2.4.2.tar.gz 47.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybencher 2.4.2
File Interpreter ABI Platform
pybencher-2.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 57.2 kB

Release files / pybencher-2.4.2.tar.gz

Download URL pybencher-2.4.2.tar.gz
Size 47.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2ff7a51af85962aee8f5f09f5b02ca6af4156abc5fbaac3efcfe6089b655020c
BLAKE2b-256 checksum
How to use checksums
98f2019d4b0bbec4a8b3422e9a533cf7c5788ec0f7cae64ce4e9865ac2768ce7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","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}

Release files / pybencher-2.4.2-py3-none-any.whl

Download URL pybencher-2.4.2-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81e4ea2139ed905e99f723b9d7e84436dd758b35c880989e261bc12fb5bfef32
BLAKE2b-256 checksum
How to use checksums
d35d487eba6348d35e7b0c69173cc9b4c23c74bbb8c8b2ed1c3b86da3eaccfdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","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}

Release history Release notifications | RSS feed

This release

2.4.2 This release

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.6

2 release files

1.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release 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