{"id":21392,"library":"google-benchmark","title":"Google Benchmark","description":"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.","status":"active","version":"1.9.5","language":"python","source_language":"en","source_url":"https://github.com/google/benchmark.git","tags":["benchmark","performance","testing","google","c++ wrapper"],"install":[{"cmd":"pip install google-benchmark","lang":"bash","label":"Default install"}],"dependencies":[{"reason":"Recommended for array-based benchmarks","package":"numpy","optional":true}],"imports":[{"note":"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.","wrong":"import benchmark","symbol":"benchmark","correct":"import google_benchmark as benchmark"}],"quickstart":{"code":"import google_benchmark as benchmark\nimport os\n\n@benchmark.register\ndef my_benchmark(state):\n    x = int(os.environ.get('SIZE', '1000'))\n    for _ in state:\n        sum(i*i for i in range(x))\n\nif __name__ == '__main__':\n    benchmark.main()","lang":"python","description":"Register a benchmark function and run with benchmark.main()."},"warnings":[{"fix":"Replace 'import benchmark' with 'import google_benchmark as benchmark'.","message":"In google-benchmark >=1.9.0, the module name changed from 'benchmark' to 'google_benchmark'. Older code using 'import benchmark' will break.","severity":"breaking","affected_versions":">=1.9.0"},{"fix":"Always include 'for _ in state:' inside your benchmark function.","message":"Using `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.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure your benchmark function accepts exactly one argument (the state object).","message":"The `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.","severity":"gotcha","affected_versions":"all"},{"fix":"Use register with custom setup/teardown inside the benchmark function or wait for new fixture API.","message":"The 'SetUp' and 'TearDown' methods via subclassing have been deprecated in favor of fixture decorators (not yet available). Check official docs for alternatives.","severity":"deprecated","affected_versions":">=1.8.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Install the correct package: pip install google-benchmark, then import as import google_benchmark as benchmark.","cause":"Trying to import 'benchmark' but the module is installed as 'google_benchmark' (package name: google-benchmark).","error":"ModuleNotFoundError: No module named 'benchmark'"},{"fix":"Uninstall the old package (pip uninstall benchmark) and install google-benchmark (pip install google-benchmark). Then use import google_benchmark as benchmark.","cause":"The 'benchmark' module is the old package (benchmark) installed, not google_benchmark.","error":"AttributeError: module 'benchmark' has no attribute 'register'"},{"fix":"Add state parameter: def my_benchmark(state): and use for _ in state: inside.","cause":"Benchmark function does not accept the state argument.","error":"TypeError: my_benchmark() takes 0 positional arguments but 1 was given"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}