Timer.h 4.0 KB

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