Created
August 29, 2018 00:29
-
-
Save alifahrri/f4444f447721ae7408f55bffc7b6aba5 to your computer and use it in GitHub Desktop.
i need to measure how long my code runs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TIMER_HPP | |
#define TIMER_HPP | |
#include <chrono> | |
template | |
< | |
typename numeric, | |
typename ratio = std::milli, | |
typename clock = std::chrono::high_resolution_clock | |
> | |
struct Timer { | |
typedef std::chrono::time_point<clock> TP; | |
typedef std::chrono::duration<numeric,ratio> D; | |
Timer() {} | |
void reset() | |
{ | |
best = 1e10; | |
worst = -1e10; | |
} | |
void start() | |
{ | |
t0 = clock::now(); | |
} | |
D stop() | |
{ | |
t1 = clock::now(); | |
auto t = dt(); | |
total += t.count(); | |
count++; | |
if(t.count() < best) | |
best = t.count(); | |
else if(t.count() > worst) | |
worst = t.count(); | |
avg = total/count; | |
return t; | |
} | |
inline | |
D dt() | |
{ | |
return t1-t0; | |
} | |
TP t0; | |
TP t1; | |
numeric best = numeric(1e10); | |
numeric worst = numeric(-1e10); | |
numeric avg = numeric(0.0); | |
numeric total = numeric(0.0); | |
int count = 0; | |
}; | |
#endif // TIMER_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment