Skip to content

Instantly share code, notes, and snippets.

@alifahrri
Created August 29, 2018 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alifahrri/f4444f447721ae7408f55bffc7b6aba5 to your computer and use it in GitHub Desktop.
Save alifahrri/f4444f447721ae7408f55bffc7b6aba5 to your computer and use it in GitHub Desktop.
i need to measure how long my code runs
#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