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.

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