Timer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // High Resolution Timer.
  2. //
  3. // Resolution on Mac (clock tick)
  4. // Resolution on Linux (1 us not tested)
  5. // Resolution on Windows (clock tick not tested)
  6. #ifndef IGL_TIMER_H
  7. #define IGL_TIMER_H
  8. #ifdef WIN32 // Windows system specific
  9. #include <windows.h>
  10. #elif __APPLE__ // Unix based system specific
  11. #include <mach/mach_time.h> // for mach_absolute_time
  12. #else
  13. #include <sys/time.h>
  14. #endif
  15. namespace igl
  16. {
  17. class Timer
  18. {
  19. public:
  20. // default constructor
  21. Timer():
  22. stopped(0),
  23. #ifdef WIN32
  24. frequency(),
  25. startCount(),
  26. endCount()
  27. #elif __APPLE__
  28. startCount(0),
  29. endCount(0)
  30. #else
  31. startCount(),
  32. endCount()
  33. #endif
  34. {
  35. #ifdef WIN32
  36. QueryPerformanceFrequency(&frequency);
  37. startCount.QuadPart = 0;
  38. endCount.QuadPart = 0;
  39. #elif __APPLE__
  40. startCount = 0;
  41. endCount = 0;
  42. #else
  43. startCount.tv_sec = startCount.tv_usec = 0;
  44. endCount.tv_sec = endCount.tv_usec = 0;
  45. #endif
  46. stopped = 0;
  47. }
  48. // default destructor
  49. ~Timer()
  50. {
  51. }
  52. #ifdef __APPLE__
  53. //Raw mach_absolute_times going in, difference in seconds out
  54. double subtractTimes( uint64_t endTime, uint64_t startTime )
  55. {
  56. uint64_t difference = endTime - startTime;
  57. static double conversion = 0.0;
  58. if( conversion == 0.0 )
  59. {
  60. mach_timebase_info_data_t info;
  61. kern_return_t err = mach_timebase_info( &info );
  62. //Convert the timebase into seconds
  63. if( err == 0 )
  64. conversion = 1e-9 * (double) info.numer / (double) info.denom;
  65. }
  66. return conversion * (double) difference;
  67. }
  68. #endif
  69. // start timer
  70. void start()
  71. {
  72. stopped = 0; // reset stop flag
  73. #ifdef WIN32
  74. QueryPerformanceCounter(&startCount);
  75. #elif __APPLE__
  76. startCount = mach_absolute_time();
  77. #else
  78. gettimeofday(&startCount, NULL);
  79. #endif
  80. }
  81. // stop the timer
  82. void stop()
  83. {
  84. stopped = 1; // set timer stopped flag
  85. #ifdef WIN32
  86. QueryPerformanceCounter(&endCount);
  87. #elif __APPLE__
  88. endCount = mach_absolute_time();
  89. #else
  90. gettimeofday(&endCount, NULL);
  91. #endif
  92. }
  93. // get elapsed time in second
  94. double getElapsedTime()
  95. {
  96. return this->getElapsedTimeInSec();
  97. }
  98. // get elapsed time in second (same as getElapsedTime)
  99. double getElapsedTimeInSec()
  100. {
  101. return this->getElapsedTimeInMicroSec() * 0.000001;
  102. }
  103. // get elapsed time in milli-second
  104. double getElapsedTimeInMilliSec()
  105. {
  106. return this->getElapsedTimeInMicroSec() * 0.001;
  107. }
  108. // get elapsed time in micro-second
  109. double getElapsedTimeInMicroSec()
  110. {
  111. double startTimeInMicroSec = 0;
  112. double endTimeInMicroSec = 0;
  113. #ifdef WIN32
  114. if(!stopped)
  115. QueryPerformanceCounter(&endCount);
  116. startTimeInMicroSec =
  117. startCount.QuadPart * (1000000.0 / frequency.QuadPart);
  118. endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
  119. #elif __APPLE__
  120. if (!stopped)
  121. endCount = mach_absolute_time();
  122. return subtractTimes(endCount,startCount)/1e-6;
  123. #else
  124. if(!stopped)
  125. gettimeofday(&endCount, NULL);
  126. startTimeInMicroSec =
  127. (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  128. endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
  129. #endif
  130. return endTimeInMicroSec - startTimeInMicroSec;
  131. }
  132. protected:
  133. private:
  134. // stop flag
  135. int stopped;
  136. #ifdef WIN32
  137. // ticks per second
  138. LARGE_INTEGER frequency;
  139. LARGE_INTEGER startCount;
  140. LARGE_INTEGER endCount;
  141. #elif __APPLE__
  142. uint64_t startCount;
  143. uint64_t endCount;
  144. #else
  145. timeval startCount;
  146. timeval endCount;
  147. #endif
  148. };
  149. }
  150. #endif // TIMER_H_DEF