0Pricing
C++ Academy · Lesson

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

  1. Cache-Friendly Data Layouts
  2. Branch Prediction and Hot Loops
  3. Profiling with perf vtune and Sanitizers
  4. Micro-benchmarking with Google Benchmark
← Back to C++ Academy