Last active
December 16, 2015 08:29
-
-
Save lorenzoriano/5406305 to your computer and use it in GitHub Desktop.
C++ class for accurate time measurement
This file contains hidden or 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_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