C++ MIT

nanobench

Simple, fast, accurate single-header microbenchmarking functionality for C++11/14/17/20

M

martinus

Dernière activité 7 sept. 2026
martinus/nanobench

1,7 k

étoiles

105

forks

0

issues ouvertes

benchmarkcppcpp11header-onlymicrobenchmarksingle-filesingle-headersingle-header-lib

Ce README est souvent en anglais.

ankerl::nanobench

ankerl::nanobench logo

Release GitHub license Build, Test, Lint Join the chat at https://gitter.im/nanobench/community

ankerl::nanobench is a platform independent microbenchmarking library for C++11/14/17/20.

#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>

int main() {
    double d = 1.0;
    ankerl::nanobench::Bench().run("some double ops", [&] {
        d += 1.0 / d;
        if (d > 5.0) {
            d -= 5.0;
        }
        ankerl::nanobench::doNotOptimizeAway(d);
    });
}

The whole executable runs for ~60ms and prints

|               ns/op |                op/s |    err% |          ins/op |          cyc/op |    IPC |         bra/op |   miss% |     total | benchmark
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
|                7.52 |      132,948,239.79 |    1.1% |            6.65 |           24.07 |  0.276 |           1.00 |    8.9% |      0.00 | `some double ops`

Which github renders as

ns/op op/s err% ins/op cyc/op IPC bra/op miss% total benchmark
7.52 132,948,239.79 1.1% 6.65 24.07 0.276 1.00 8.9% 0.00 some double ops

The benchmarked code takes 7.52 nanoseconds to run, so ~133 million times per seconds. Measurements fluctuate by 1.1%. On average 6.65 instructions are executed in 24.07 CPU cycles, resulting in 0.276 instructions per cycle. A single branch is in the code, which branch prediction missed in 8.9% of the cases. Total runtime of the benchmark with the name some double ops is 0.00, so just a few milliseconds.

Design Goals

Documentation

Extensive documentation is available.

More

Projets similaires

A micro microbenchmarking library for C++11 in a single header file

C++
IiboB
234 étoiles25

A microbenchmark support library

C++benchmark
Ggoogle
10,4 k étoiles1,8 k

⏱️ single header benchmark framework for C and C++

C++benchmarkbenchmark-frameworkc
Ssheredom
267 étoiles24