diff options
author | Cory Fields <cory-nospam-@coryfields.com> | 2017-10-25 16:38:24 -0400 |
---|---|---|
committer | Cory Fields <cory-nospam-@coryfields.com> | 2017-11-07 17:15:58 -0500 |
commit | c515d266ec04d7f0b2b1b3815a793c27ddcd4e1c (patch) | |
tree | 57055da9437118e4220868fee4ce3853534cb7c3 /src/bench/bench.h | |
parent | 57ee73990f1ce29916adfd99f93eae1ccea1a43b (diff) |
bench: switch to std::chrono for time measurements
std::chrono removes portability issues.
Rather than storing doubles, store the untouched time_points. Then
convert to nanoseconds for display. This allows for maximum precision, while
keeping results comparable between differing hardware/operating systems.
Also, display full nanosecond counts rather than sub-second floats.
Diffstat (limited to 'src/bench/bench.h')
-rw-r--r-- | src/bench/bench.h | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/bench/bench.h b/src/bench/bench.h index 79109eaa56..d7037e1f33 100644 --- a/src/bench/bench.h +++ b/src/bench/bench.h @@ -9,6 +9,7 @@ #include <limits> #include <map> #include <string> +#include <chrono> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/stringize.hpp> @@ -37,11 +38,15 @@ BENCHMARK(CODE_TO_TIME); namespace benchmark { + using clock = std::chrono::high_resolution_clock; + using time_point = clock::time_point; + using duration = clock::duration; + class State { std::string name; - double maxElapsed; - double beginTime; - double lastTime, minTime, maxTime; + duration maxElapsed; + time_point beginTime, lastTime; + duration minTime, maxTime; uint64_t count; uint64_t countMask; uint64_t beginCycles; @@ -49,9 +54,9 @@ namespace benchmark { uint64_t minCycles; uint64_t maxCycles; 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(); + State(std::string _name, duration _maxElapsed) : name(_name), maxElapsed(_maxElapsed), count(0) { + minTime = duration::max(); + maxTime = duration::zero(); minCycles = std::numeric_limits<uint64_t>::max(); maxCycles = std::numeric_limits<uint64_t>::min(); countMask = 1; @@ -69,7 +74,7 @@ namespace benchmark { public: BenchRunner(std::string name, BenchFunction func); - static void RunAll(double elapsedTimeForOne=1.0); + static void RunAll(duration elapsedTimeForOne = std::chrono::seconds(1)); }; } |