Timer.h 4.2 KB

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