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. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Micro-benchmarking with Google Benchmark” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Micro-benchmarking with Google Benchmark”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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