Google Benchmark
A library to benchmark code snippets, similar to unit tests. - google/benchmark
Prefer Nanobench
More complete than folly benchmark. Simpler to setup.
Note
use -fno-omit-frame-pointer
to conserve callstack with optimized code
show perf
and `perf report -g ‘graph,0.5,caller’ to invert graph reported
define
- UNLIKELY -
__builtin_expected((bool)(x), 0)
Disabling CPU Frequency Scaling
sudo cpupower frequency-set --governor performance
./mybench
sudo cpupower frequency-set --governor powersave
Preventing Optimization
- DoNotOptimize(
) - ClobberMemory()
Some implementation
- escape - tell compiler that referenced object is modified (create/reserve test)
equivalent ofdoNotOptimizeAway
from folly.
static void escape(void *p) {
asm volatile("" : : "g"(p) : "memory");
}
- clobber - tell the compiler that whole memory is modified (push_back test).
static void clobber() {
asm volatile("" : : : "memory");
}
BENCHMARK
#include <benchmark/benchmark.h>
static void BM_SomeFunction(benchmark::State& state) {
// Perform setup here
for (auto _ : state) {
// This code gets timed
SomeFunction();
}
}
// Register the function as a benchmark
BENCHMARK(BM_SomeFunction);
// Run the benchmark
BENCHMARK_MAIN();
BENCHMARK
loop can be customized.
Single Range
BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
BENCHMARK(BM_memcpy)->Range(8, 8<<10);
BENCHMARK(BM_memcpy)->RangeMultiplier(2)->Range(8, 8<<10);
BENCHMARK(BM_DenseRange)->DenseRange(0, 1024, 128);
Dual Range
Install
work with Meson
## google benchmark
opt_var = cmake.subproject_options()
opt_var.add_cmake_defines({'BENCHMARK_ENABLE_GTEST_TESTS': 'OFF'})
libgooglebench = cmake.subproject('libgoogle_benchmark', options: opt_var)
executable('bench_pickmove',
[ 'bench/bench_pickmove.cc'],
cpp_args : ['-g', '-save-temps'],
install : true,
dependencies: [ libgooglebench.dependency('benchmark')]
)
subprojects/libgoogle_benchmark.wrap
[wrap-git]
url = https://github.com/google/benchmark.git
revision = v1.6.0
Written on November 12, 2021, Last update on October 26, 2022
c++
lib
fastware
benchmarking