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
This commit is contained in:
Michael Cook (mackal) 2016-05-13 21:33:03 -04:00
parent cb39a35f3f
commit edc42bf5b6

View File

@ -19,6 +19,7 @@
#define TIMER_H
#include "types.h"
#include <chrono>
// 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<double> (clock::now() - start_time).count(); }
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
};
#endif