Skip to content

Instantly share code, notes, and snippets.

@lorenzoriano
Last active December 16, 2015 08:29
Show Gist options
  • Save lorenzoriano/5406305 to your computer and use it in GitHub Desktop.
Save lorenzoriano/5406305 to your computer and use it in GitHub Desktop.
C++ class for accurate time measurement
#ifndef TIMER_H
#define TIMER_H
#include <ctime>
//link with rt (-lrt)
class timer {
public:
timer() {
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start_time_);
}
void restart() {
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start_time_);
}
// return elapsed time in seconds
double elapsed() const {
timespec curr_time;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &curr_time);
return double(curr_time.tv_nsec - start_time_.tv_nsec) / 1.0e9 +
double(curr_time.tv_sec - start_time_.tv_sec);
}
private:
timespec start_time_;
}; // timer
#endif // TIMER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment