美文网首页
使用google-benchmark测量函数运行时间

使用google-benchmark测量函数运行时间

作者: louyang | 来源:发表于2019-10-17 14:50 被阅读0次

在Fedora 31上安装google-benchmark,

dnf install google-benchmark-devel

假设有个简单函数需要测量,如下:

int sum(int a, int b)
{
    return a + b;
}

加入google benchmark的代码:

int sum(int a, int b)
{
    return a + b;
}


#include <benchmark/benchmark.h>

void wrapper(benchmark::State& state)
{
    for (auto _ : state) {
        sum(1,2);
    }
}
BENCHMARK(wrapper);

BENCHMARK_MAIN();

编译运行:

$ g++ a.cpp -lbenchmark && ./a.out
2019-10-17 02:49:36
Running ./a.out
Run on (2 X 2496 MHz CPU s)
CPU Caches:
  L1 Data 32K (x2)
  L1 Instruction 32K (x2)
  L2 Unified 256K (x2)
  L3 Unified 3072K (x2)
Load Average: 0.01, 0.03, 0.00
-----------------------------------------------------
Benchmark           Time             CPU   Iterations
-----------------------------------------------------
wrapper          4.78 ns         4.66 ns    154096424

相关文章

网友评论

      本文标题:使用google-benchmark测量函数运行时间

      本文链接:https://www.haomeiwen.com/subject/ivommctx.html