|home| |posts| |projects| |cv| |bookmarks| |github|

Benchmark Cpp Code

C++ code can be benchmarked by using the Google benchmark library.

Install library

Go to the latest release, download and unpack the tarball:

wget https://github.com/google/benchmark/archive/refs/tags/v1.5.4.tar.gz
tar xf v1.5.4.tar.gz
cd benchmark-1.5.4

Build and install the library:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_TESTING=OFF -G Ninja
cmake --build build
sudo cmake --build build --target install
sudo ldconfig

Usage

Let's say we want to see how fast is std::string allocation.

A simple benchmark that measures string allocation would be something like this:

#include <benchmark/benchmark.h>

#include <string>

static void BM_string(benchmark::State& state) {
  auto len = state.range(0);
  for (auto _ : state) {
      std::string s(len, 'x');
  }
}

BENCHMARK(BM_string)->DenseRange(0,20);

BENCHMARK_MAIN();

Let's break it down line by line:

#include <benchmark/benchmark.h>
static void BM_string(benchmark::State& state) {
  auto len = state.range(0);
  for (auto _ : state) {
      std::string s(len, 'x');
  }
}

What happens before the loop is not measured and therefore is the appropriate place to perform setup operations(in this case just get the arg passed at registration).

What happens in the loop will be measured. The loop will be executed a number of times until the results are statistically stable.

BENCHMARK(BM_string)->DenseRange(0,32);
BENCHMARK_MAIN();

Now we need to compile the code:

clang++ -O3 -o bench_example bench_example.cpp -lbenchmark -lpthread

And run the benchmark:

./bench_example