Timer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Timer.h
  3. // =======
  4. // High Resolution Timer.
  5. // This timer is able to measure the elapsed time with 1 micro-second accuracy
  6. // in both Windows, Linux and Unix system
  7. //
  8. // AUTHOR: Song Ho Ahn (song.ahn@gmail.com)
  9. // CREATED: 2003-01-13
  10. // UPDATED: 2006-01-13
  11. //
  12. // Copyright (c) 2003 Song Ho Ahn
  13. //////////////////////////////////////////////////////////////////////////////
  14. #ifndef IGL_TIMER_H
  15. #define IGL_TIMER_H
  16. #ifdef WIN32 // Windows system specific
  17. #include <windows.h>
  18. #else // Unix based system specific
  19. #include <sys/time.h>
  20. #endif
  21. namespace igl
  22. {
  23. class Timer
  24. {
  25. public:
  26. Timer() // default constructor
  27. {
  28. #ifdef WIN32
  29. QueryPerformanceFrequency(&frequency);
  30. startCount.QuadPart = 0;
  31. endCount.QuadPart = 0;
  32. #else
  33. startCount.tv_sec = startCount.tv_usec = 0;
  34. endCount.tv_sec = endCount.tv_usec = 0;
  35. #endif
  36. stopped = 0;
  37. startTimeInMicroSec = 0;
  38. endTimeInMicroSec = 0;
  39. }
  40. ~Timer() // default destructor
  41. {
  42. }
  43. void start() // start timer
  44. {
  45. stopped = 0; // reset stop flag
  46. #ifdef WIN32
  47. QueryPerformanceCounter(&startCount);
  48. #else
  49. gettimeofday(&startCount, NULL);
  50. #endif
  51. }
  52. void stop() // stop the timer
  53. {
  54. stopped = 1; // set timer stopped flag
  55. #ifdef WIN32
  56. QueryPerformanceCounter(&endCount);
  57. #else
  58. gettimeofday(&endCount, NULL);
  59. #endif
  60. }
  61. double getElapsedTime() // get elapsed time in second
  62. {
  63. return this->getElapsedTimeInSec();
  64. }
  65. double getElapsedTimeInSec() // get elapsed time in second (same as getElapsedTime)
  66. {
  67. return this->getElapsedTimeInMicroSec() * 0.000001;
  68. }
  69. double getElapsedTimeInMilliSec() // get elapsed time in milli-second
  70. {
  71. return this->getElapsedTimeInMicroSec() * 0.001;
  72. }
  73. double getElapsedTimeInMicroSec() // get elapsed time in micro-second
  74. {
  75. #ifdef WIN32
  76. if(!stopped)
  77. QueryPerformanceCounter(&endCount);
  78. startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
  79. endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
  80. #else
  81. if(!stopped)
  82. gettimeofday(&endCount, NULL);
  83. startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  84. endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
  85. #endif
  86. return endTimeInMicroSec - startTimeInMicroSec;
  87. }
  88. protected:
  89. private:
  90. double startTimeInMicroSec; // starting time in micro-second
  91. double endTimeInMicroSec; // ending time in micro-second
  92. int stopped; // stop flag
  93. #ifdef WIN32
  94. LARGE_INTEGER frequency; // ticks per second
  95. LARGE_INTEGER startCount; //
  96. LARGE_INTEGER endCount; //
  97. #else
  98. timeval startCount; //
  99. timeval endCount; //
  100. #endif
  101. };
  102. }
  103. #endif // TIMER_H_DEF