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.
diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h
index a6015b8..23dd3d0 100644
--- a/include/benchmark/benchmark.h
+++ b/include/benchmark/benchmark.h
@@ -575,7 +575,10 @@
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)
diff --git a/test/basic_test.cc b/test/basic_test.cc
index 100f689..d07fbc0 100644
--- a/test/basic_test.cc
+++ b/test/basic_test.cc
@@ -99,6 +99,7 @@
void BM_KeepRunning(benchmark::State& state) {
size_t iter_count = 0;
+ assert(iter_count == state.iterations());
while (state.KeepRunning()) {
++iter_count;
}