From edc42bf5b694b84b8d6e0295a76aaf17c7f40d09 Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Fri, 13 May 2016 21:33:03 -0400 Subject: [PATCH] Add small chrono timer object This is just so if someone wants a quick way to measure how long something takes for benchmarking purposes they don't have to reinvent anything. See examples in comments --- common/timer.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/common/timer.h b/common/timer.h index 4b2719dc6..f06f5bbe4 100644 --- a/common/timer.h +++ b/common/timer.h @@ -19,6 +19,7 @@ #define TIMER_H #include "types.h" +#include // Disgrace: for windows compile #ifdef _WINDOWS @@ -65,4 +66,28 @@ private: bool pUseAcurateTiming; }; +/* Wrapper around chrono to make adding simple time based benching easy + * ex: + * void foo() { + * ... + * BenchTimer timer; + * ... (expensive work here) + * auto dur = timer.elapsed(); + * std::cout << "foo() took " << dur << seconds" << std::endl; + * ... + * } + * */ + +struct BenchTimer +{ + typedef std::chrono::high_resolution_clock clock; + + BenchTimer() : start_time(clock::now()) {} + void reset() { start_time = clock::now(); } + // this is seconds + double elapsed() { return std::chrono::duration (clock::now() - start_time).count(); } +private: + std::chrono::time_point start_time; +}; + #endif