Timer.h 4.1 KB

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