Guest User

Untitled

a guest
Jan 7th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #ifndef TIMER_H_DEF
  2. #define TIMER_H_DEF
  3.  
  4. #ifdef WIN32   // Windows system specific
  5. #include <windows.h>
  6. #else          // Unix based system specific
  7. #include <sys/time.h>
  8. #endif
  9.  
  10. class Timer {
  11.     public:
  12.         Timer();      
  13.         ~Timer();    
  14.         void start();    
  15.         void stop();  
  16.         double getElapsedTime();
  17.         double getElapsedTimeInSec();  
  18.         double getElapsedTimeInMilliSec();    
  19.         double getElapsedTimeInMicroSec();  
  20.    
  21.     private:
  22.         double startTimeInMicroSec;
  23.         double endTimeInMicroSec;
  24.         int stopped;                            
  25.     #ifdef WIN32
  26.         LARGE_INTEGER frequency;                  
  27.         LARGE_INTEGER startCount;                  
  28.         LARGE_INTEGER endCount;  
  29.     #else
  30.         timeval startCount;          
  31.         timeval endCount;
  32.     #endif
  33. };
  34.  
  35. Timer::Timer(){
  36.     #ifdef WIN32
  37.     QueryPerformanceFrequency(&frequency);
  38.     startCount.QuadPart = 0;
  39.     endCount.QuadPart = 0;
  40.     #else
  41.     startCount.tv_sec = startCount.tv_usec = 0;
  42.     endCount.tv_sec = endCount.tv_usec = 0;
  43.     #endif
  44.     stopped = 0;
  45.     startTimeInMicroSec = 0;
  46.     endTimeInMicroSec = 0;
  47. }
  48.  
  49.  
  50. Timer::~Timer(){}
  51.  
  52. void Timer::start(){
  53.     stopped = 0;
  54.     #ifdef WIN32
  55.     QueryPerformanceCounter(&startCount);
  56.     #else
  57.     gettimeofday(&startCount, NULL);
  58.     #endif
  59. }
  60.  
  61. void Timer::stop(){
  62.     stopped = 1; // set timer stopped flag
  63.     #ifdef WIN32
  64.     QueryPerformanceCounter(&endCount);
  65.     #else
  66.     gettimeofday(&endCount, NULL);
  67.     #endif
  68. }
  69.  
  70. double Timer::getElapsedTimeInMicroSec(){
  71.     #ifdef WIN32
  72.     if(!stopped) QueryPerformanceCounter(&endCount);
  73.     startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
  74.     endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
  75.     #else
  76.     if(!stopped) gettimeofday(&endCount, NULL);
  77.     startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  78.     endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
  79.     #endif
  80.     return endTimeInMicroSec - startTimeInMicroSec;
  81. }
  82.  
  83. double Timer::getElapsedTimeInMilliSec(){
  84.     return this->getElapsedTimeInMicroSec() * 0.001;
  85. }
  86.  
  87. double Timer::getElapsedTimeInSec(){
  88.     return this->getElapsedTimeInMicroSec() * 0.000001;
  89. }
  90.  
  91. double Timer::getElapsedTime(){
  92.     return this->getElapsedTimeInSec();
  93. }
  94.  
  95. #endif // TIMER_H_DEF
Advertisement
Add Comment
Please, Sign In to add comment