Registry / testing / google-benchmark

google-benchmark

JSON →
library1.9.5pypypi✓ verified 84d ago

A Python library to benchmark code snippets, wrapping the C++ Google Benchmark library. Version 1.9.5 requires Python >=3.10 and provides a simple API for timing code execution and comparing performance.

pip install google-benchmark
INSTALL
IMPORT
SIG · GOOGLE-BENCHMARK
G
google-benchmark
testingpythonv1.9.5
Install
1.8s avg
Import
55ms
Disk
17MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.9.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.044s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

benchmark
import google_benchmark as benchmark
import benchmark
The package name on PyPI is google-benchmark, but the module is google_benchmark. Using 'import benchmark' will import the separate 'benchmark' package if installed, which is different.

Register a benchmark function and run with benchmark.main().

import google_benchmark as benchmark import os @benchmark.register def my_benchmark(state): x = int(os.environ.get('SIZE', '1000')) for _ in state: sum(i*i for i in range(x)) if __name__ == '__main__': benchmark.main()
Debug
Known issues
breakingIn google-benchmark >=1.9.0, the module name changed from 'benchmark' to 'google_benchmark'. Older code using 'import benchmark' will break.
fix
Replace 'import benchmark' with 'import google_benchmark as benchmark'.
affects: >=1.9.0
gotchaUsing `for _ in state:` is required to iterate the number of times determined by the framework. Forgetting this loop will cause the benchmark to run only once and produce incorrect results.
fix
Always include 'for _ in state:' inside your benchmark function.
affects: all
gotchaThe `state` object must be passed as the first argument to the benchmark function. Changing the argument name is fine, but omitting it or using a different signature will raise a TypeError.
fix
Ensure your benchmark function accepts exactly one argument (the state object).
affects: all
deprecatedThe 'SetUp' and 'TearDown' methods via subclassing have been deprecated in favor of fixture decorators (not yet available). Check official docs for alternatives.
fix
Use register with custom setup/teardown inside the benchmark function or wait for new fixture API.
affects: >=1.8.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'benchmark'
Trying to import 'benchmark' but the module is installed as 'google_benchmark' (package name: google-benchmark).
fix
Install the correct package: pip install google-benchmark, then import as import google_benchmark as benchmark.
AttributeError: module 'benchmark' has no attribute 'register'
The 'benchmark' module is the old package (benchmark) installed, not google_benchmark.
fix
Uninstall the old package (pip uninstall benchmark) and install google-benchmark (pip install google-benchmark). Then use import google_benchmark as benchmark.
TypeError: my_benchmark() takes 0 positional arguments but 1 was given
Benchmark function does not accept the state argument.
fix
Add state parameter: def my_benchmark(state): and use for _ in state: inside.
Upgrade
Version history
1.9.5latest on PyPI · released Feb 2, 2026
Audit
Dependencies
numpyoptionalRecommended for array-based benchmarks
Agent activity
12 hits · last 30 days
node
10
Bingbot
1
Resources
google-benchmark — pip install google-benchmark · libregistry