benchmarkinggoogle-benchmark

How can I tell Google Benchmark to not benchmark a line of code?


I am using Google Benchmark to benchmark a library. I have the benchmark set up as follows:

for (auto _ : state) {
    run_function(first, last, v);
}

What I would like is for v to be randomly generated every iteration so that I could get a range of benchmark values and obtain the statistics from them. I can do this via:

std::random_device rand_dev;
std::mt19937 generator(rand_dev());
std::uniform_int_distribution<int>  distr(min, max);
for (auto _ : state) {
    v = distr(generator)
    run_function(first, last, v);
}

Some of the functions I am testing are on the order of 10-100ns, so adding in the generator has a significant effect on the results. Is there any way to tell Google Bench to skip a line/block of code?


Solution

  • You can use the PauseTiming and ResumeTiming methods on the State object to pause and resume the timing. However, it may introduce some overhead to the timing loop. If the function under benchmark is very fast you may notice it.