Return 0 from State::iterations() when not yet started. (#598)

* Return a reasonable value from State::iterations() even before starting a benchmark

* Optimize State::iterations() for started case.
This commit is contained in:
Samuel Panzer 2018-05-24 02:33:19 -07:00 committed by Dominic Hamon
parent 6d74c0625b
commit ce3fde16cb
2 changed files with 5 additions and 1 deletions

View File

@ -575,7 +575,10 @@ class State {
BENCHMARK_ALWAYS_INLINE
size_t iterations() const {
return (max_iterations - total_iterations_ + batch_leftover_);
if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
return 0;
}
return max_iterations - total_iterations_ + batch_leftover_;
}
private: // items we expect on the first cache line (ie 64 bytes of the struct)

View File

@ -99,6 +99,7 @@ BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
void BM_KeepRunning(benchmark::State& state) {
size_t iter_count = 0;
assert(iter_count == state.iterations());
while (state.KeepRunning()) {
++iter_count;
}