benchmark
A microbenchmark support library
Google's C++ library for measuring how fast specific pieces of code run, wrap a function in a benchmark, compile it, and get nanosecond-level timing with statistical reliability to find and verify performance improvements.
Google Benchmark is a C++ library that measures how fast specific pieces of code run. Think of it as a timing system for developers: you wrap the code you want to measure in a small function, register it, and the library runs it many times to produce reliable timing data. This is called microbenchmarking, and it helps developers find slow spots and verify that a code change actually made things faster.
The library works similarly in structure to unit testing frameworks. You write benchmark functions, mark them with a special macro, and then run the compiled program. The output shows how long each operation took, typically in nanoseconds, along with how many times it ran per second. You can benchmark things as specific as creating a string or copying a data structure.
Installing it requires a C++ build setup. The README walks through the steps using CMake, a common build tool: clone the repository, configure the build, compile the library, and then link your own code against it. It works on Linux, macOS, and Windows. There is also support for Python bindings if you want to benchmark Python code using the same framework.
The project is maintained by Google and has an active community with a discussion group and a Discord channel. The main branch is the stable version, while a separate experimental branch called v2 is where newer features are tested. The library requires a C++ compiler that supports C++17 to build, though it can be used in C++11 projects once compiled.
Where it fits
- Measure whether a code change actually made a C++ function faster before merging it to the main branch.
- Add a benchmark suite to a C++ library to automatically catch performance regressions in CI.
- Find the slowest operations in a codebase by timing individual functions at nanosecond resolution.
- Use the Python bindings to benchmark Python code with the same statistical timing framework.