You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.3 KiB

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