How to special-case the number of iterations in google benchmark?

Viewed 2317

I am aware of the --benchmark_repetitions flag and it is not what I need. I want to be able to specify the number of iterations for one benchmark. I am okay with using a --benchmark_iterations flag which sets the number of iterations for all benchmarks.

I know google benchmark is smart to figure out how many iterations is needed to get a good measurement. This is good enough for most use cases but my use case is different. I need to be able to control the number of iterations precisely.

2 Answers

BENCHMARK(YourBenchmark)->Iterations(num_ite); will do the trick.

You can specify iterations for each benchmark.

If you are using BENCHMARK_F, do that in the constructor

class BenchmarkBase : public benchmark::Fixture {
public:

    BenchmarkBase() {
        Iterations(num_ite);
    }
};

It doesn't support the case of wanting to tune the iteration count precisely.

As you've pointed out, it runs until there are enough iterations to get a good enough signal (though it doesn't yet actually check the statistics). We used to have a way to set the iteration counts, but the interaction with the timing related flags are complex and were confusing to people trying to configure their benchmarks, so they were removed.

It is something that comes up often though, and if there is a way we could support iterations and timing together, or support iterations but also warn if we think the result isn't meaningful, then i'm open to considering it.

Related