Nanobench

a platform independent microbenchmarking library for C++11/14/17/20. - nanobench comparison / r/cpp / 2

Single header code, easier to integrate and faster than google benchmark.

pyperf

Tune the system for benchmarks

python3 -m pip install pyperf

nanobench

#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>

#include <atomic>

int main() {
    int y = 0;
    std::atomic<int> x(0);
    ankerl::nanobench::Bench().run("compare_exchange_strong", [&] {
        x.compare_exchange_strong(y, 0);
    });
}

Output

Warning, results might be unstable:
* CPU frequency scaling enabled: CPU 0 between 400.0 and 4,900.0 MHz
* CPU governor is 'powersave' but should be 'performance'
* Turbo is enabled, CPU frequency will fluctuate

Recommendations
* Use 'pyperf system tune' before benchmarking. See https://github.com/psf/pyperf

|               ns/op |                op/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|               18.97 |       52,724,029.52 |    1.2% |      0.01 | `compare_exchange_strong`

see also

  • Micro-benchmarks - compare Google Benchmark / Catch2 / Hayai / Celero / Nonius
Written on November 13, 2021, Last update on November 23, 2022
benchmarking c++ single-header