Timer.h 4.4 KB

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