BenchTimer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_BENCH_TIMERR_H
  11. #define EIGEN_BENCH_TIMERR_H
  12. #if defined(_WIN32) || defined(__CYGWIN__)
  13. # ifndef NOMINMAX
  14. # define NOMINMAX
  15. # define EIGEN_BT_UNDEF_NOMINMAX
  16. # endif
  17. # ifndef WIN32_LEAN_AND_MEAN
  18. # define WIN32_LEAN_AND_MEAN
  19. # define EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
  20. # endif
  21. # include <windows.h>
  22. #elif defined(__APPLE__)
  23. #include <mach/mach_time.h>
  24. #else
  25. # include <unistd.h>
  26. #endif
  27. static void escape(void *p) {
  28. #if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
  29. asm volatile("" : : "g"(p) : "memory");
  30. #endif
  31. }
  32. static void clobber() {
  33. #if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
  34. asm volatile("" : : : "memory");
  35. #endif
  36. }
  37. #include <Eigen/Core>
  38. namespace Eigen
  39. {
  40. enum {
  41. CPU_TIMER = 0,
  42. REAL_TIMER = 1
  43. };
  44. /** Elapsed time timer keeping the best try.
  45. *
  46. * On POSIX platforms we use clock_gettime with CLOCK_PROCESS_CPUTIME_ID.
  47. * On Windows we use QueryPerformanceCounter
  48. *
  49. * Important: on linux, you must link with -lrt
  50. */
  51. class BenchTimer
  52. {
  53. public:
  54. BenchTimer()
  55. {
  56. #if defined(_WIN32) || defined(__CYGWIN__)
  57. LARGE_INTEGER freq;
  58. QueryPerformanceFrequency(&freq);
  59. m_frequency = (double)freq.QuadPart;
  60. #endif
  61. reset();
  62. }
  63. ~BenchTimer() {}
  64. inline void reset()
  65. {
  66. m_bests.fill(1e9);
  67. m_worsts.fill(0);
  68. m_totals.setZero();
  69. }
  70. inline void start()
  71. {
  72. m_starts[CPU_TIMER] = getCpuTime();
  73. m_starts[REAL_TIMER] = getRealTime();
  74. }
  75. inline void stop()
  76. {
  77. m_times[CPU_TIMER] = getCpuTime() - m_starts[CPU_TIMER];
  78. m_times[REAL_TIMER] = getRealTime() - m_starts[REAL_TIMER];
  79. #if EIGEN_VERSION_AT_LEAST(2,90,0)
  80. m_bests = m_bests.cwiseMin(m_times);
  81. m_worsts = m_worsts.cwiseMax(m_times);
  82. #else
  83. m_bests(0) = std::min(m_bests(0),m_times(0));
  84. m_bests(1) = std::min(m_bests(1),m_times(1));
  85. m_worsts(0) = std::max(m_worsts(0),m_times(0));
  86. m_worsts(1) = std::max(m_worsts(1),m_times(1));
  87. #endif
  88. m_totals += m_times;
  89. }
  90. /** Return the elapsed time in seconds between the last start/stop pair
  91. */
  92. inline double value(int TIMER = CPU_TIMER) const
  93. {
  94. return m_times[TIMER];
  95. }
  96. /** Return the best elapsed time in seconds
  97. */
  98. inline double best(int TIMER = CPU_TIMER) const
  99. {
  100. return m_bests[TIMER];
  101. }
  102. /** Return the worst elapsed time in seconds
  103. */
  104. inline double worst(int TIMER = CPU_TIMER) const
  105. {
  106. return m_worsts[TIMER];
  107. }
  108. /** Return the total elapsed time in seconds.
  109. */
  110. inline double total(int TIMER = CPU_TIMER) const
  111. {
  112. return m_totals[TIMER];
  113. }
  114. inline double getCpuTime() const
  115. {
  116. #ifdef _WIN32
  117. LARGE_INTEGER query_ticks;
  118. QueryPerformanceCounter(&query_ticks);
  119. return query_ticks.QuadPart/m_frequency;
  120. #elif __APPLE__
  121. return double(mach_absolute_time())*1e-9;
  122. #else
  123. timespec ts;
  124. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  125. return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
  126. #endif
  127. }
  128. inline double getRealTime() const
  129. {
  130. #ifdef _WIN32
  131. SYSTEMTIME st;
  132. GetSystemTime(&st);
  133. return (double)st.wSecond + 1.e-3 * (double)st.wMilliseconds;
  134. #elif __APPLE__
  135. return double(mach_absolute_time())*1e-9;
  136. #else
  137. timespec ts;
  138. clock_gettime(CLOCK_REALTIME, &ts);
  139. return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
  140. #endif
  141. }
  142. protected:
  143. #if defined(_WIN32) || defined(__CYGWIN__)
  144. double m_frequency;
  145. #endif
  146. Vector2d m_starts;
  147. Vector2d m_times;
  148. Vector2d m_bests;
  149. Vector2d m_worsts;
  150. Vector2d m_totals;
  151. public:
  152. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  153. };
  154. #define BENCH(TIMER,TRIES,REP,CODE) { \
  155. TIMER.reset(); \
  156. for(int uglyvarname1=0; uglyvarname1<TRIES; ++uglyvarname1){ \
  157. TIMER.start(); \
  158. for(int uglyvarname2=0; uglyvarname2<REP; ++uglyvarname2){ \
  159. CODE; \
  160. } \
  161. TIMER.stop(); \
  162. clobber(); \
  163. } \
  164. }
  165. }
  166. // clean #defined tokens
  167. #ifdef EIGEN_BT_UNDEF_NOMINMAX
  168. # undef EIGEN_BT_UNDEF_NOMINMAX
  169. # undef NOMINMAX
  170. #endif
  171. #ifdef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
  172. # undef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
  173. # undef WIN32_LEAN_AND_MEAN
  174. #endif
  175. #endif // EIGEN_BENCH_TIMERR_H