Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef EIGEN_BENCH_TIMERR_H
00027 #define EIGEN_BENCH_TIMERR_H
00028
00029 #if defined(_WIN32) || defined(__CYGWIN__)
00030 # ifndef NOMINMAX
00031 # define NOMINMAX
00032 # define EIGEN_BT_UNDEF_NOMINMAX
00033 # endif
00034 # ifndef WIN32_LEAN_AND_MEAN
00035 # define WIN32_LEAN_AND_MEAN
00036 # define EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
00037 # endif
00038 # include <windows.h>
00039 #else
00040 # include <unistd.h>
00041 #endif
00042
00043 #include <Eigen/Core>
00044
00045 namespace Eigen
00046 {
00047
00048 enum {
00049 CPU_TIMER = 0,
00050 REAL_TIMER = 1
00051 };
00052
00060 class BenchTimer
00061 {
00062 public:
00063
00064 BenchTimer()
00065 {
00066 #if defined(_WIN32) || defined(__CYGWIN__)
00067 LARGE_INTEGER freq;
00068 QueryPerformanceFrequency(&freq);
00069 m_frequency = (double)freq.QuadPart;
00070 #endif
00071 reset();
00072 }
00073
00074 ~BenchTimer() {}
00075
00076 inline void reset()
00077 {
00078 m_bests.fill(1e9);
00079 m_totals.setZero();
00080 }
00081 inline void start()
00082 {
00083 m_starts[CPU_TIMER] = getCpuTime();
00084 m_starts[REAL_TIMER] = getRealTime();
00085 }
00086 inline void stop()
00087 {
00088 m_times[CPU_TIMER] = getCpuTime() - m_starts[CPU_TIMER];
00089 m_times[REAL_TIMER] = getRealTime() - m_starts[REAL_TIMER];
00090 #if EIGEN_VERSION_AT_LEAST(2,90,0)
00091 m_bests = m_bests.cwiseMin(m_times);
00092 #else
00093 m_bests(0) = std::min(m_bests(0),m_times(0));
00094 m_bests(1) = std::min(m_bests(1),m_times(1));
00095 #endif
00096 m_totals += m_times;
00097 }
00098
00101 inline double value(int TIMER = CPU_TIMER)
00102 {
00103 return m_times[TIMER];
00104 }
00105
00108 inline double best(int TIMER = CPU_TIMER)
00109 {
00110 return m_bests[TIMER];
00111 }
00112
00115 inline double total(int TIMER = CPU_TIMER)
00116 {
00117 return m_totals[TIMER];
00118 }
00119
00120 inline double getCpuTime()
00121 {
00122 #ifdef _WIN32
00123 LARGE_INTEGER query_ticks;
00124 QueryPerformanceCounter(&query_ticks);
00125 return query_ticks.QuadPart/m_frequency;
00126 #else
00127 timespec ts;
00128 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
00129 return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
00130 #endif
00131 }
00132
00133 inline double getRealTime()
00134 {
00135 #ifdef _WIN32
00136 SYSTEMTIME st;
00137 GetSystemTime(&st);
00138 return (double)st.wSecond + 1.e-3 * (double)st.wMilliseconds;
00139 #else
00140 timespec ts;
00141 clock_gettime(CLOCK_REALTIME, &ts);
00142 return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
00143 #endif
00144 }
00145
00146 protected:
00147 #if defined(_WIN32) || defined(__CYGWIN__)
00148 double m_frequency;
00149 #endif
00150 Vector2d m_starts;
00151 Vector2d m_times;
00152 Vector2d m_bests;
00153 Vector2d m_totals;
00154
00155 };
00156
00157 #define BENCH(TIMER,TRIES,REP,CODE) { \
00158 TIMER.reset(); \
00159 for(int uglyvarname1=0; uglyvarname1<TRIES; ++uglyvarname1){ \
00160 TIMER.start(); \
00161 for(int uglyvarname2=0; uglyvarname2<REP; ++uglyvarname2){ \
00162 CODE; \
00163 } \
00164 TIMER.stop(); \
00165 } \
00166 }
00167
00168 }
00169
00170
00171 #ifdef EIGEN_BT_UNDEF_NOMINMAX
00172 # undef EIGEN_BT_UNDEF_NOMINMAX
00173 # undef NOMINMAX
00174 #endif
00175
00176 #ifdef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
00177 # undef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
00178 # undef WIN32_LEAN_AND_MEAN
00179 #endif
00180
00181 #endif // EIGEN_BENCH_TIMERR_H