Created
August 7, 2012 17:03
-
-
Save grodtron/3287323 to your computer and use it in GitHub Desktop.
A cpp timer class that provides nanosecond resolution
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
/* | |
* A class that provides a simple timer | |
* Works in Windows and Linux, resolution is hardware dependant | |
* | |
* | |
* Copyleft Gordon Bailey 2012 - All wrongs reserved | |
* | |
* */ | |
#include "timer.h" | |
#ifdef _WIN32 | |
#include <cstdlib> | |
#include <iostream> | |
using std::endl; | |
using std::cerr; | |
timer::timer() : ready(false) { | |
bool success = QueryPerformanceFrequency(&counter_freq); | |
if(!success){ | |
cerr << "Error, QueryPerformanceFrequency failed." << endl; | |
system("pause"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
void timer::start() { | |
ready = false; | |
bool success = QueryPerformanceCounter(&start_time); | |
if(!success){ | |
cerr << "Error, QueryPerformanceCounter failed." << endl; | |
system("pause"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
void timer::end(){ | |
ready = true; | |
bool success = QueryPerformanceCounter(&end_time); | |
if(!success){ | |
cerr << "Error, QueryPerformanceCounter failed." << endl; | |
system("pause"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
// there is probably some unnecessary typecasting here, but better safe than sorry | |
double timer::duration(){ | |
if (ready){ | |
return 1e6 * (((double)(end_time.QuadPart - start_time.QuadPart)) / ((double)counter_freq.QuadPart)); | |
}else{ | |
return 0; | |
} | |
} | |
#else | |
timer::timer() : ready(false) {} | |
void timer::start() { | |
ready = false; | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time); | |
} | |
void timer::end(){ | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time); | |
ready = true; | |
} | |
double timer::duration(){ | |
if (ready){ | |
return 1e6 * (difftime(end_time.tv_sec, start_time.tv_sec) + (1e-9 * (double)(end_time.tv_nsec - start_time.tv_nsec))); | |
}else{ | |
return 0; | |
} | |
} | |
#endif |
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_H | |
#define TIMER_H | |
#ifdef _WIN32 | |
#include <Windows.h> | |
#else | |
#include <time.h> | |
#endif | |
class timer{ | |
#ifdef _WIN32 | |
LARGE_INTEGER start_time; | |
LARGE_INTEGER end_time; | |
LARGE_INTEGER counter_freq; | |
#else | |
struct timespec start_time; | |
struct timespec end_time; | |
#endif | |
bool ready; | |
public: | |
void start(); | |
void end(); | |
double duration(); | |
timer(); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment