HighResolutionTimer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //////////////////////////////////////////////////////////////////////////////
  2. // HighResolutionTimer.cpp
  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 ([email protected])
  9. // CREATED: 2003-01-13
  10. // UPDATED: 2006-01-13
  11. //
  12. // Copyright (c) 2003 Song Ho Ahn
  13. //////////////////////////////////////////////////////////////////////////////
  14. #include "HighResolutionTimer.h"
  15. #include <stdlib.h>
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // constructor
  18. ///////////////////////////////////////////////////////////////////////////////
  19. HighResolutionTimer::HighResolutionTimer()
  20. {
  21. #ifdef WIN32
  22. QueryPerformanceFrequency(&frequency);
  23. startCount.QuadPart = 0;
  24. endCount.QuadPart = 0;
  25. #else
  26. startCount.tv_sec = startCount.tv_usec = 0;
  27. endCount.tv_sec = endCount.tv_usec = 0;
  28. #endif
  29. stopped = 0;
  30. startTimeInMicroSec = 0;
  31. endTimeInMicroSec = 0;
  32. }
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // distructor
  35. ///////////////////////////////////////////////////////////////////////////////
  36. HighResolutionTimer::~HighResolutionTimer()
  37. {
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // start timer.
  41. // startCount will be set at this point.
  42. ///////////////////////////////////////////////////////////////////////////////
  43. void HighResolutionTimer::start()
  44. {
  45. stopped = 0; // reset stop flag
  46. #ifdef WIN32
  47. QueryPerformanceCounter(&startCount);
  48. #else
  49. gettimeofday(&startCount, NULL);
  50. #endif
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // stop the timer.
  54. // endCount will be set at this point.
  55. ///////////////////////////////////////////////////////////////////////////////
  56. void HighResolutionTimer::stop()
  57. {
  58. stopped = 1; // set timer stopped flag
  59. #ifdef WIN32
  60. QueryPerformanceCounter(&endCount);
  61. #else
  62. gettimeofday(&endCount, NULL);
  63. #endif
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // compute elapsed time in micro-second resolution.
  67. // other getElapsedTime will call this first, then convert to correspond resolution.
  68. ///////////////////////////////////////////////////////////////////////////////
  69. double HighResolutionTimer::getElapsedTimeInMicroSec()
  70. {
  71. #ifdef WIN32
  72. if(!stopped)
  73. QueryPerformanceCounter(&endCount);
  74. startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
  75. endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
  76. #else
  77. if(!stopped)
  78. gettimeofday(&endCount, NULL);
  79. startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  80. endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
  81. #endif
  82. return endTimeInMicroSec - startTimeInMicroSec;
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // divide elapsedTimeInMicroSec by 1000
  86. ///////////////////////////////////////////////////////////////////////////////
  87. double HighResolutionTimer::getElapsedTimeInMilliSec()
  88. {
  89. return this->getElapsedTimeInMicroSec() * 0.001;
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // divide elapsedTimeInMicroSec by 1000000
  93. ///////////////////////////////////////////////////////////////////////////////
  94. double HighResolutionTimer::getElapsedTimeInSec()
  95. {
  96. return this->getElapsedTimeInMicroSec() * 0.000001;
  97. }
  98. ///////////////////////////////////////////////////////////////////////////////
  99. // same as getElapsedTimeInSec()
  100. ///////////////////////////////////////////////////////////////////////////////
  101. double HighResolutionTimer::getElapsedTime()
  102. {
  103. return this->getElapsedTimeInSec();
  104. }