Micro-benchmarking with Google Benchmark
Write reliable micro-benchmarks with Google Benchmark and avoid measurement pitfalls.
Micro-benchmarking with Google Benchmark is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Micro-benchmark?
Macro-profiling shows where time is spent. Micro-benchmarks measure specific code in isolation — useful for comparing algorithms or implementations.
Google Benchmark Setup
Header-only-ish C++ benchmark library by Google. Install via vcpkg, Conan, or apt; link -lbenchmark.
#include <benchmark/benchmark.h>
static void BM_StringCopy(benchmark::State& state) {
std::string s = "Hello, World!";
for (auto _ : state) {
std::string copy(s);
}
}
BENCHMARK(BM_StringCopy);
BENCHMARK_MAIN();Compiling and Running
Compile with optimization, link with the benchmark library, and run.
g++ -O2 -DNDEBUG bench.cpp -lbenchmark -lpthread -o bench
./benchOutput Interpretation
The library prints time per iteration, iteration count, and CPU time. Compare baselines with proposed changes.
Avoiding Optimizer Tricks
The optimizer can delete useless code. Use benchmark::DoNotOptimize to force the result to "matter."
for (auto _ : state) {
auto result = expensive_function();
benchmark::DoNotOptimize(result);
}Preventing Dead Stores
benchmark::ClobberMemory tells the compiler the memory state has been observed — prevents store elimination.
Parameterized Benchmarks
Test the same function across different input sizes.
static void BM_Sort(benchmark::State& state) {
std::vector<int> v(state.range(0));
std::iota(v.rbegin(), v.rend(), 0);
for (auto _ : state) {
auto copy = v;
std::sort(copy.begin(), copy.end());
}
}
BENCHMARK(BM_Sort)->RangeMultiplier(2)->Range(8, 1<<14);Multithreaded Benchmarks
Test scalability with the Threads modifier.
BENCHMARK(BM_ConcurrentMap)->Threads(1)->Threads(4)->Threads(16);Custom Counters
Track domain-specific metrics like bytes/sec or items/sec.
state.SetItemsProcessed(state.iterations() * input.size());
state.SetBytesProcessed(state.iterations() * input.size() * sizeof(int));Statistical Output
Run with --benchmark_repetitions=10 to get mean, median, and standard deviation. The library reports them automatically.
Common Pitfalls
Watch out for:
- Compiler eliminating "dead" code → use DoNotOptimize
- Warm-up effects → measure many iterations
- CPU frequency scaling → pin the core and disable boost
- Other processes running → benchmark on a quiet machine
Reading Numbers Critically
A 5% improvement might be measurement noise. Run multiple repetitions; only believe statistically significant differences. Compare against a known baseline.
Alternatives
Other micro-benchmark tools:
- Nonius — header-only C++ benchmark
- Hayai — older but lightweight
- Catch2 has built-in benchmark support
Quick Check
Why use benchmark::DoNotOptimize(result) inside a benchmark loop?
Recap
Use Google Benchmark for fine-grained measurements. Prevent compiler tricks with DoNotOptimize and ClobberMemory. Use parameterized and multithreaded benchmarks to explore behavior across inputs and concurrency. Believe only statistically significant differences.
Frequently asked questions
Is the “Micro-benchmarking with Google Benchmark” lesson free?
Yes — the full text of “Micro-benchmarking with Google Benchmark” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Micro-benchmarking with Google Benchmark”?
Write reliable micro-benchmarks with Google Benchmark and avoid measurement pitfalls. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Micro-benchmarking with Google Benchmark” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Cache-Friendly Data Layouts
- Branch Prediction and Hot Loops
- Profiling with perf vtune and Sanitizers
- Micro-benchmarking with Google Benchmark