BenchTimer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. asm volatile("" : : "g"(p) : "memory");
  29. }
  30. static void clobber() {
  31. asm volatile("" : : : "memory");
  32. }
  33. #include <Eigen/Core>
  34. namespace Eigen
  35. {
  36. enum {
  37. CPU_TIMER = 0,
  38. REAL_TIMER = 1
  39. };
  40. /** Elapsed time timer keeping the best try.
  41. *
  42. * On POSIX platforms we use clock_gettime with CLOCK_PROCESS_CPUTIME_ID.
  43. * On Windows we use QueryPerformanceCounter
  44. *
  45. * Important: on linux, you must link with -lrt
  46. */
  47. class BenchTimer
  48. {
  49. public:
  50. BenchTimer()
  51. {
  52. #if defined(_WIN32) || defined(__CYGWIN__)
  53. LARGE_INTEGER freq;
  54. QueryPerformanceFrequency(&freq);
  55. m_frequency = (double)freq.QuadPart;
  56. #endif
  57. reset();
  58. }
  59. ~BenchTimer() {}
  60. inline void reset()
  61. {
  62. m_bests.fill(1e9);
  63. m_worsts.fill(0);
  64. m_totals.setZero();
  65. }
  66. inline void start()
  67. {
  68. m_starts[CPU_TIMER] = getCpuTime();
  69. m_starts[REAL_TIMER] = getRealTime();
  70. }
  71. inline void stop()
  72. {
  73. m_times[CPU_TIMER] = getCpuTime() - m_starts[CPU_TIMER];
  74. m_times[REAL_TIMER] = getRealTime() - m_starts[REAL_TIMER];
  75. #if EIGEN_VERSION_AT_LEAST(2,90,0)
  76. m_bests = m_bests.cwiseMin(m_times);
  77. m_worsts = m_worsts.cwiseMax(m_times);
  78. #else
  79. m_bests(0) = std::min(m_bests(0),m_times(0));
  80. m_bests(1) = std::min(m_bests(1),m_times(1));
  81. m_worsts(0) = std::max(m_worsts(0),m_times(0));
  82. m_worsts(1) = std::max(m_worsts(1),m_times(1));
  83. #endif
  84. m_totals += m_times;
  85. }
  86. /** Return the elapsed time in seconds between the last start/stop pair
  87. */
  88. inline double value(int TIMER = CPU_TIMER) const
  89. {
  90. return m_times[TIMER];
  91. }
  92. /** Return the best elapsed time in seconds
  93. */
  94. inline double best(int TIMER = CPU_TIMER) const
  95. {
  96. return m_bests[TIMER];
  97. }
  98. /** Return the worst elapsed time in seconds
  99. */
  100. inline double worst(int TIMER = CPU_TIMER) const
  101. {
  102. return m_worsts[TIMER];
  103. }
  104. /** Return the total elapsed time in seconds.
  105. */
  106. inline double total(int TIMER = CPU_TIMER) const
  107. {
  108. return m_totals[TIMER];
  109. }
  110. inline double getCpuTime() const
  111. {
  112. #ifdef _WIN32
  113. LARGE_INTEGER query_ticks;
  114. QueryPerformanceCounter(&query_ticks);
  115. return query_ticks.QuadPart/m_frequency;
  116. #elif __APPLE__
  117. return double(mach_absolute_time())*1e-9;
  118. #else
  119. timespec ts;
  120. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  121. return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
  122. #endif
  123. }
  124. inline double getRealTime() const
  125. {
  126. #ifdef _WIN32
  127. SYSTEMTIME st;
  128. GetSystemTime(&st);
  129. return (double)st.wSecond + 1.e-3 * (double)st.wMilliseconds;
  130. #elif __APPLE__
  131. return double(mach_absolute_time())*1e-9;
  132. #else
  133. timespec ts;
  134. clock_gettime(CLOCK_REALTIME, &ts);
  135. return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
  136. #endif
  137. }
  138. protected:
  139. #if defined(_WIN32) || defined(__CYGWIN__)
  140. double m_frequency;
  141. #endif
  142. Vector2d m_starts;
  143. Vector2d m_times;
  144. Vector2d m_bests;
  145. Vector2d m_worsts;
  146. Vector2d m_totals;
  147. public:
  148. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  149. };
  150. #define BENCH(TIMER,TRIES,REP,CODE) { \
  151. TIMER.reset(); \
  152. for(int uglyvarname1=0; uglyvarname1<TRIES; ++uglyvarname1){ \
  153. TIMER.start(); \
  154. for(int uglyvarname2=0; uglyvarname2<REP; ++uglyvarname2){ \
  155. CODE; \
  156. } \
  157. TIMER.stop(); \
  158. clobber(); \
  159. } \
  160. }
  161. }
  162. // clean #defined tokens
  163. #ifdef EIGEN_BT_UNDEF_NOMINMAX
  164. # undef EIGEN_BT_UNDEF_NOMINMAX
  165. # undef NOMINMAX
  166. #endif
  167. #ifdef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
  168. # undef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
  169. # undef WIN32_LEAN_AND_MEAN
  170. #endif
  171. #endif // EIGEN_BENCH_TIMERR_H