// https://github.com/martinus/nanobench
// g++ -O2 -I../../include main.cpp -o m
#define ANKERL_NANOBENCH_IMPLEMENT
#include<nanobench.h>#include<chrono>#include<random>#include<thread>intmain(int,char**){uint64_tx=1;ankerl::nanobench::Bench().run("x += x",[&](){ankerl::nanobench::doNotOptimizeAway(x+=x);});ankerl::nanobench::Bench().run("sleep 10ms",[&](){std::this_thread::sleep_for(std::chrono::milliseconds(10));});std::random_devicedev;std::mt19937_64rng(dev());ankerl::nanobench::Bench().minEpochIterations(12045).run("random fluctuations",[&](){// each run, perform a random number of rng calls
autoiterations=rng()&UINT64_C(0xff);for(uint64_ti=0;i<iterations;++i){(void)rng();}});}
编译以上代码,然后运行即可得到结果
1
2
3
4
5
6
7
8
9
10
11
12
Warning, results might be unstable:
* CPU frequency scaling enabled: CPU 0 between 800.0 and 4,800.0 MHz
* 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
|--------------------:|--------------------:|--------:|----------:|:----------
| 0.40 | 2,485,348,469.62 | 2.6% | 0.01 |`x+= x`| 10,124,540.00 | 98.77 | 0.0% | 0.11 |`sleep 10ms`| 237.14 | 4,216,912.81 | 4.2% | 0.04 |`random fluctuations`