explaingit

google/benchmark

10,197C++Audience · developerComplexity · 3/5Setup · moderate

TLDR

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.

Mindmap

mindmap
  root((repo))
    What it does
      Microbenchmarking
      Nanosecond timing
      Performance regression
    How it works
      Wrap code in function
      Register with macro
      Run many iterations
      Statistical output
    Setup
      CMake build system
      C++17 compiler
      Python bindings
    Platform Support
      Linux
      macOS
      Windows
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Measure whether a code change actually made a C++ function faster before merging it to the main branch.

USE CASE 2

Add a benchmark suite to a C++ library to automatically catch performance regressions in CI.

USE CASE 3

Find the slowest operations in a codebase by timing individual functions at nanosecond resolution.

USE CASE 4

Use the Python bindings to benchmark Python code with the same statistical timing framework.

Tech stack

C++CMakePython

Getting it running

Difficulty · moderate Time to first run · 30min

Requires CMake and a C++17-compatible compiler, the library links as a static dependency to your own C++ project.

In plain English

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.

Copy-paste prompts

Prompt 1
Using google/benchmark, write a C++ benchmark that compares std::vector vs std::list for inserting 1000 integers and report the results.
Prompt 2
Set up google/benchmark with CMake in an existing C++17 project, show me the CMakeLists.txt additions needed to link and compile it.
Prompt 3
Add a parameterized google/benchmark that tests a string search function with input sizes ranging from 10 to 100000 characters.
Prompt 4
How do I use google/benchmark's Python bindings to benchmark a Python function and get nanosecond-level timing output?
Prompt 5
Set up a GitHub Actions CI job that runs google/benchmark on every PR and fails if any benchmark regresses by more than 5%.
Open on GitHub → Explain another repo

← google on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.