Micro-benchmarking with Google Benchmark
Write reliable micro-benchmarks with Google Benchmark and avoid measurement pitfalls.
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();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