Timer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. {
  23. #ifdef WIN32
  24. QueryPerformanceFrequency(&frequency);
  25. startCount.QuadPart = 0;
  26. endCount.QuadPart = 0;
  27. #elif __APPLE__
  28. startCount = 0;
  29. endCount = 0;
  30. #else
  31. startCount.tv_sec = startCount.tv_usec = 0;
  32. endCount.tv_sec = endCount.tv_usec = 0;
  33. #endif
  34. stopped = 0;
  35. }
  36. // default destructor
  37. ~Timer()
  38. {
  39. }
  40. #ifdef __APPLE__
  41. //Raw mach_absolute_times going in, difference in seconds out
  42. double subtractTimes( uint64_t endTime, uint64_t startTime )
  43. {
  44. uint64_t difference = endTime - startTime;
  45. static double conversion = 0.0;
  46. if( conversion == 0.0 )
  47. {
  48. mach_timebase_info_data_t info;
  49. kern_return_t err = mach_timebase_info( &info );
  50. //Convert the timebase into seconds
  51. if( err == 0 )
  52. conversion = 1e-9 * (double) info.numer / (double) info.denom;
  53. }
  54. return conversion * (double) difference;
  55. }
  56. #endif
  57. // start timer
  58. void start()
  59. {
  60. stopped = 0; // reset stop flag
  61. #ifdef WIN32
  62. QueryPerformanceCounter(&startCount);
  63. #elif __APPLE__
  64. startCount = mach_absolute_time();
  65. #else
  66. gettimeofday(&startCount, NULL);
  67. #endif
  68. }
  69. // stop the timer
  70. void stop()
  71. {
  72. stopped = 1; // set timer stopped flag
  73. #ifdef WIN32
  74. QueryPerformanceCounter(&endCount);
  75. #elif __APPLE__
  76. endCount = mach_absolute_time();
  77. #else
  78. gettimeofday(&endCount, NULL);
  79. #endif
  80. }
  81. // get elapsed time in second
  82. double getElapsedTime()
  83. {
  84. return this->getElapsedTimeInSec();
  85. }
  86. // get elapsed time in second (same as getElapsedTime)
  87. double getElapsedTimeInSec()
  88. {
  89. return this->getElapsedTimeInMicroSec() * 0.000001;
  90. }
  91. // get elapsed time in milli-second
  92. double getElapsedTimeInMilliSec()
  93. {
  94. return this->getElapsedTimeInMicroSec() * 0.001;
  95. }
  96. // get elapsed time in micro-second
  97. double getElapsedTimeInMicroSec()
  98. {
  99. double startTimeInMicroSec = 0;
  100. double endTimeInMicroSec = 0;
  101. #ifdef WIN32
  102. if(!stopped)
  103. QueryPerformanceCounter(&endCount);
  104. startTimeInMicroSec =
  105. startCount.QuadPart * (1000000.0 / frequency.QuadPart);
  106. endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
  107. #elif __APPLE__
  108. if (!stopped)
  109. endCount = mach_absolute_time();
  110. return subtractTimes(endCount,startCount)/1e-6;
  111. #else
  112. if(!stopped)
  113. gettimeofday(&endCount, NULL);
  114. startTimeInMicroSec =
  115. (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  116. endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
  117. #endif
  118. return endTimeInMicroSec - startTimeInMicroSec;
  119. }
  120. protected:
  121. private:
  122. // stop flag
  123. int stopped;
  124. #ifdef WIN32
  125. // ticks per second
  126. LARGE_INTEGER frequency;
  127. LARGE_INTEGER startCount;
  128. LARGE_INTEGER endCount;
  129. #elif __APPLE__
  130. uint64_t startCount;
  131. uint64_t endCount;
  132. #else
  133. timeval startCount;
  134. timeval endCount;
  135. #endif
  136. };
  137. }
  138. #endif // TIMER_H_DEF