Commit ce3fde16 by Samuel Panzer Committed by Dominic Hamon

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.
parent 6d74c062
...@@ -575,7 +575,10 @@ class State { ...@@ -575,7 +575,10 @@ class State {
BENCHMARK_ALWAYS_INLINE BENCHMARK_ALWAYS_INLINE
size_t iterations() const { 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) private: // items we expect on the first cache line (ie 64 bytes of the struct)
......
...@@ -99,6 +99,7 @@ BENCHMARK(BM_empty_stop_start)->ThreadPerCpu(); ...@@ -99,6 +99,7 @@ BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
void BM_KeepRunning(benchmark::State& state) { void BM_KeepRunning(benchmark::State& state) {
size_t iter_count = 0; size_t iter_count = 0;
assert(iter_count == state.iterations());
while (state.KeepRunning()) { while (state.KeepRunning()) {
++iter_count; ++iter_count;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment