Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TIMER_H_DEF
- #define TIMER_H_DEF
- #ifdef WIN32 // Windows system specific
- #include <windows.h>
- #else // Unix based system specific
- #include <sys/time.h>
- #endif
- class Timer {
- public:
- Timer();
- ~Timer();
- void start();
- void stop();
- double getElapsedTime();
- double getElapsedTimeInSec();
- double getElapsedTimeInMilliSec();
- double getElapsedTimeInMicroSec();
- private:
- double startTimeInMicroSec;
- double endTimeInMicroSec;
- int stopped;
- #ifdef WIN32
- LARGE_INTEGER frequency;
- LARGE_INTEGER startCount;
- LARGE_INTEGER endCount;
- #else
- timeval startCount;
- timeval endCount;
- #endif
- };
- Timer::Timer(){
- #ifdef WIN32
- QueryPerformanceFrequency(&frequency);
- startCount.QuadPart = 0;
- endCount.QuadPart = 0;
- #else
- startCount.tv_sec = startCount.tv_usec = 0;
- endCount.tv_sec = endCount.tv_usec = 0;
- #endif
- stopped = 0;
- startTimeInMicroSec = 0;
- endTimeInMicroSec = 0;
- }
- Timer::~Timer(){}
- void Timer::start(){
- stopped = 0;
- #ifdef WIN32
- QueryPerformanceCounter(&startCount);
- #else
- gettimeofday(&startCount, NULL);
- #endif
- }
- void Timer::stop(){
- stopped = 1; // set timer stopped flag
- #ifdef WIN32
- QueryPerformanceCounter(&endCount);
- #else
- gettimeofday(&endCount, NULL);
- #endif
- }
- double Timer::getElapsedTimeInMicroSec(){
- #ifdef WIN32
- if(!stopped) QueryPerformanceCounter(&endCount);
- startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
- endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
- #else
- if(!stopped) gettimeofday(&endCount, NULL);
- startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
- endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
- #endif
- return endTimeInMicroSec - startTimeInMicroSec;
- }
- double Timer::getElapsedTimeInMilliSec(){
- return this->getElapsedTimeInMicroSec() * 0.001;
- }
- double Timer::getElapsedTimeInSec(){
- return this->getElapsedTimeInMicroSec() * 0.000001;
- }
- double Timer::getElapsedTime(){
- return this->getElapsedTimeInSec();
- }
- #endif // TIMER_H_DEF
Advertisement
Add Comment
Please, Sign In to add comment