00001 // from: http://snipplr.com/view/40650/timer-class-for-both-unixlinuxmac-and-windows-system/ 00002 00004 // How to Use //// 00006 00007 //#include <iostream> 00008 //#include "timer.h" 00009 //using namespace std; 00010 00011 //int main() 00012 //{ 00013 // Timer timer; 00014 // 00015 // // start timer 00016 // timer.start(); 00017 // 00018 // // do something 00019 // ... 00020 // 00021 // // stop timer 00022 // timer.stop(); 00023 // 00024 // // print the elapsed time in millisec 00025 // cout << timer.getElapsedTimeInMilliSec() << " ms.\n"; 00026 // 00027 // return 0; 00028 //} 00029 00030 00032 // Timer.h 00033 // ======= 00034 // High Resolution Timer. 00035 // This timer is able to measure the elapsed time with 1 micro-second accuracy 00036 // in both Windows, Linux and Unix system 00037 // 00038 // AUTHOR: Song Ho Ahn (song.ahn@gmail.com) 00039 // CREATED: 2003-01-13 00040 // UPDATED: 2006-01-13 00041 // 00042 // Copyright (c) 2003 Song Ho Ahn 00044 00045 #ifndef TIMER_H_DEF 00046 #define TIMER_H_DEF 00047 00048 #ifndef __LINUX__ // Windows system specific 00049 #include <windows.h> 00050 #else // Unix based system specific 00051 #include <sys/time.h> 00052 #endif 00053 00054 #include <stdlib.h> 00055 00056 00057 class Timer 00058 { 00059 public: 00060 // default constructor 00061 Timer() 00062 { 00063 #ifdef WIN32 00064 QueryPerformanceFrequency(&frequency); 00065 startCount.QuadPart = 0; 00066 endCount.QuadPart = 0; 00067 #else 00068 startCount.tv_sec = startCount.tv_usec = 0; 00069 endCount.tv_sec = endCount.tv_usec = 0; 00070 #endif 00071 00072 stopped = 0; 00073 startTimeInMicroSec = 0; 00074 endTimeInMicroSec = 0; 00075 } 00076 00077 // default destructor 00078 ~Timer() 00079 {} 00080 00082 // start timer. 00083 // startCount will be set at this point. 00085 void start() 00086 { 00087 stopped = 0; // reset stop flag 00088 #ifdef WIN32 00089 QueryPerformanceCounter(&startCount); 00090 #else 00091 gettimeofday(&startCount, NULL); 00092 #endif 00093 } 00094 00096 // stop the timer. 00097 // endCount will be set at this point. 00099 void stop() 00100 { 00101 stopped = 1; // set timer stopped flag 00102 00103 #ifdef WIN32 00104 QueryPerformanceCounter(&endCount); 00105 #else 00106 gettimeofday(&endCount, NULL); 00107 #endif 00108 } 00109 00111 // same as getElapsedTimeInSec() 00113 double getElapsedTime() 00114 { 00115 return this->getElapsedTimeInSec(); 00116 } 00117 00119 // divide elapsedTimeInMicroSec by 1000000 00121 double getElapsedTimeInSec() 00122 { 00123 return this->getElapsedTimeInMicroSec() * 0.000001; 00124 } 00125 00127 // divide elapsedTimeInMicroSec by 1000 00129 double getElapsedTimeInMilliSec() 00130 { 00131 return this->getElapsedTimeInMicroSec() * 0.001; 00132 } 00133 00135 // compute elapsed time in micro-second resolution. 00136 // other getElapsedTime will call this first, then convert to correspond resolution. 00138 double getElapsedTimeInMicroSec() 00139 { 00140 #ifdef WIN32 00141 if(!stopped) 00142 QueryPerformanceCounter(&endCount); 00143 00144 startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart); 00145 endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart); 00146 #else 00147 if(!stopped) 00148 gettimeofday(&endCount, NULL); 00149 00150 startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec; 00151 endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec; 00152 #endif 00153 00154 return endTimeInMicroSec - startTimeInMicroSec; 00155 } 00156 00157 00158 protected: 00159 00160 00161 private: 00162 double startTimeInMicroSec; // starting time in micro-second 00163 double endTimeInMicroSec; // ending time in micro-second 00164 int stopped; // stop flag 00165 #ifdef WIN32 00166 LARGE_INTEGER frequency; // ticks per second 00167 LARGE_INTEGER startCount; // 00168 LARGE_INTEGER endCount; // 00169 #else 00170 timeval startCount; // 00171 timeval endCount; // 00172 #endif 00173 }; 00174 00175 #endif // TIMER_H_DEF