diff options
Diffstat (limited to 'src/bench')
-rw-r--r-- | src/bench/Examples.cpp (renamed from src/bench/MilliSleep.cpp) | 18 | ||||
-rw-r--r-- | src/bench/bench.cpp | 14 | ||||
-rw-r--r-- | src/bench/bench.h | 2 |
3 files changed, 31 insertions, 3 deletions
diff --git a/src/bench/MilliSleep.cpp b/src/bench/Examples.cpp index 991397a234..b6b020a971 100644 --- a/src/bench/MilliSleep.cpp +++ b/src/bench/Examples.cpp @@ -6,6 +6,8 @@ #include "main.h" #include "utiltime.h" +// Sanity test: this should loop ten times, and +// min/max/average should be close to 100ms. static void Sleep100ms(benchmark::State& state) { while (state.KeepRunning()) { @@ -14,3 +16,19 @@ static void Sleep100ms(benchmark::State& state) } BENCHMARK(Sleep100ms); + +// Extremely fast-running benchmark: +#include <math.h> + +volatile double sum = 0.0; // volatile, global so not optimized away + +static void Trig(benchmark::State& state) +{ + double d = 0.01; + while (state.KeepRunning()) { + sum += sin(d); + d += 0.000001; + } +} + +BENCHMARK(Trig); diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index 5720807609..89c3b0cc2a 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -36,14 +36,22 @@ BenchRunner::RunAll(double elapsedTimeForOne) bool State::KeepRunning() { - double now = gettimedouble(); + double now; if (count == 0) { - beginTime = now; + beginTime = now = gettimedouble(); } else { - double elapsedOne = now - lastTime; + // timeCheckCount is used to avoid calling gettime most of the time, + // so benchmarks that run very quickly get consistent results. + if ((count+1)%timeCheckCount != 0) { + ++count; + return true; // keep going + } + now = gettimedouble(); + double elapsedOne = (now - lastTime)/timeCheckCount; if (elapsedOne < minTime) minTime = elapsedOne; if (elapsedOne > maxTime) maxTime = elapsedOne; + if (elapsedOne*timeCheckCount < maxElapsed/16) timeCheckCount *= 2; } lastTime = now; ++count; diff --git a/src/bench/bench.h b/src/bench/bench.h index fee9b8c38d..bf591a2be6 100644 --- a/src/bench/bench.h +++ b/src/bench/bench.h @@ -41,10 +41,12 @@ namespace benchmark { double beginTime; double lastTime, minTime, maxTime; int64_t count; + int64_t timeCheckCount; public: State(std::string _name, double _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) { minTime = std::numeric_limits<double>::max(); maxTime = std::numeric_limits<double>::min(); + timeCheckCount = 1; } bool KeepRunning(); }; |