Timer Class for both Unix/Linux/Mac and Windows system


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //////////////////
  2. // How to Use ////
  3. //////////////////
  4.  
  5. #include <iostream>
  6. #include "Timer.h"
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. Timer timer;
  12.  
  13. // start timer
  14. timer.start();
  15.  
  16. // do something
  17. ...
  18.  
  19. // stop timer
  20. timer.stop();
  21.  
  22. // print the elapsed time in millisec
  23. cout << timer.getElapsedTimeInMilliSec() << " ms.\n";
  24.  
  25. return 0;
  26. }
  27.  
  28.  
  29. //////////////////////////////////////////////////////////////////////////////
  30. // Timer.h
  31. // =======
  32. // High Resolution Timer.
  33. // This timer is able to measure the elapsed time with 1 micro-second accuracy
  34. // in both Windows, Linux and Unix system
  35. //
  36. // AUTHOR: Song Ho Ahn ([email protected])
  37. // CREATED: 2003-01-13
  38. // UPDATED: 2006-01-13
  39. //
  40. // Copyright (c) 2003 Song Ho Ahn
  41. //////////////////////////////////////////////////////////////////////////////
  42.  
  43. #ifndef TIMER_H_DEF
  44. #define TIMER_H_DEF
  45.  
  46. #ifdef WIN32 // Windows system specific
  47. #include <windows.h>
  48. #else // Unix based system specific
  49. #include <sys/time.h>
  50. #endif
  51.  
  52.  
  53. class Timer
  54. {
  55. public:
  56. Timer(); // default constructor
  57. ~Timer(); // default destructor
  58.  
  59. void start(); // start timer
  60. void stop(); // stop the timer
  61. double getElapsedTime(); // get elapsed time in second
  62. double getElapsedTimeInSec(); // get elapsed time in second (same as getElapsedTime)
  63. double getElapsedTimeInMilliSec(); // get elapsed time in milli-second
  64. double getElapsedTimeInMicroSec(); // get elapsed time in micro-second
  65.  
  66.  
  67. protected:
  68.  
  69.  
  70. private:
  71. double startTimeInMicroSec; // starting time in micro-second
  72. double endTimeInMicroSec; // ending time in micro-second
  73. int stopped; // stop flag
  74. #ifdef WIN32
  75. LARGE_INTEGER frequency; // ticks per second
  76. LARGE_INTEGER startCount; //
  77. LARGE_INTEGER endCount; //
  78. #else
  79. timeval startCount; //
  80. timeval endCount; //
  81. #endif
  82. };
  83.  
  84. #endif // TIMER_H_DEF
  85.  
  86.  
  87. //////////////////////////////////////////////////////////////////////////////
  88. // Timer.cpp
  89. // =========
  90. // High Resolution Timer.
  91. // This timer is able to measure the elapsed time with 1 micro-second accuracy
  92. // in both Windows, Linux and Unix system
  93. //
  94. // AUTHOR: Song Ho Ahn ([email protected])
  95. // CREATED: 2003-01-13
  96. // UPDATED: 2006-01-13
  97. //
  98. // Copyright (c) 2003 Song Ho Ahn
  99. //////////////////////////////////////////////////////////////////////////////
  100.  
  101. #include "Timer.h"
  102. #include <stdlib.h>
  103.  
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // constructor
  106. ///////////////////////////////////////////////////////////////////////////////
  107. Timer::Timer()
  108. {
  109. #ifdef WIN32
  110. QueryPerformanceFrequency(&frequency);
  111. startCount.QuadPart = 0;
  112. endCount.QuadPart = 0;
  113. #else
  114. startCount.tv_sec = startCount.tv_usec = 0;
  115. endCount.tv_sec = endCount.tv_usec = 0;
  116. #endif
  117.  
  118. stopped = 0;
  119. startTimeInMicroSec = 0;
  120. endTimeInMicroSec = 0;
  121. }
  122.  
  123.  
  124.  
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // distructor
  127. ///////////////////////////////////////////////////////////////////////////////
  128. Timer::~Timer()
  129. {
  130. }
  131.  
  132.  
  133.  
  134. ///////////////////////////////////////////////////////////////////////////////
  135. // start timer.
  136. // startCount will be set at this point.
  137. ///////////////////////////////////////////////////////////////////////////////
  138. void Timer::start()
  139. {
  140. stopped = 0; // reset stop flag
  141. #ifdef WIN32
  142. QueryPerformanceCounter(&startCount);
  143. #else
  144. gettimeofday(&startCount, NULL);
  145. #endif
  146. }
  147.  
  148.  
  149.  
  150. ///////////////////////////////////////////////////////////////////////////////
  151. // stop the timer.
  152. // endCount will be set at this point.
  153. ///////////////////////////////////////////////////////////////////////////////
  154. void Timer::stop()
  155. {
  156. stopped = 1; // set timer stopped flag
  157.  
  158. #ifdef WIN32
  159. QueryPerformanceCounter(&endCount);
  160. #else
  161. gettimeofday(&endCount, NULL);
  162. #endif
  163. }
  164.  
  165.  
  166.  
  167. ///////////////////////////////////////////////////////////////////////////////
  168. // compute elapsed time in micro-second resolution.
  169. // other getElapsedTime will call this first, then convert to correspond resolution.
  170. ///////////////////////////////////////////////////////////////////////////////
  171. double Timer::getElapsedTimeInMicroSec()
  172. {
  173. #ifdef WIN32
  174. if(!stopped)
  175. QueryPerformanceCounter(&endCount);
  176.  
  177. startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
  178. endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
  179. #else
  180. if(!stopped)
  181. gettimeofday(&endCount, NULL);
  182.  
  183. startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  184. endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
  185. #endif
  186.  
  187. return endTimeInMicroSec - startTimeInMicroSec;
  188. }
  189.  
  190.  
  191.  
  192. ///////////////////////////////////////////////////////////////////////////////
  193. // divide elapsedTimeInMicroSec by 1000
  194. ///////////////////////////////////////////////////////////////////////////////
  195. double Timer::getElapsedTimeInMilliSec()
  196. {
  197. return this->getElapsedTimeInMicroSec() * 0.001;
  198. }
  199.  
  200.  
  201.  
  202. ///////////////////////////////////////////////////////////////////////////////
  203. // divide elapsedTimeInMicroSec by 1000000
  204. ///////////////////////////////////////////////////////////////////////////////
  205. double Timer::getElapsedTimeInSec()
  206. {
  207. return this->getElapsedTimeInMicroSec() * 0.000001;
  208. }
  209.  
  210.  
  211.  
  212. ///////////////////////////////////////////////////////////////////////////////
  213. // same as getElapsedTimeInSec()
  214. ///////////////////////////////////////////////////////////////////////////////
  215. double Timer::getElapsedTime()
  216. {
  217. return this->getElapsedTimeInSec();
  218. }

URL: http://www.songho.ca/misc/timer/timer.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.