Search code examples
c++microbenchmarkgoogle-benchmark

How to force google benchmark to run multiple iterations?


I use google benchmark to test two C++ functions. One runs for ~630,000,000 ns and one runs for ~1,000,000,000 ns. But both only run one iteration. How can I force the benchmark to run more iterations? I would like it to run at least 10 times.


Solution

  • For those who encounter similar problems:

    There is no way to directly control the number of iterations for a benchmark. Instead, one can use benchmark_min_time to indirectly configure the number of iterations a benchmark runs. A simple way to do this is:

    BENCHMARK(YourBenchmark)->MinTime(10); // 10 seconds