1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
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/.
11 #ifndef EIGEN_BENCH_TIMERR_H
12 #define EIGEN_BENCH_TIMERR_H
14 #if defined(_WIN32) || defined(__CYGWIN__)
17 # define EIGEN_BT_UNDEF_NOMINMAX
19 # ifndef WIN32_LEAN_AND_MEAN
20 # define WIN32_LEAN_AND_MEAN
21 # define EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
24 #elif defined(__APPLE__)
25 #include <CoreServices/CoreServices.h>
26 #include <mach/mach_time.h>
41 /** Elapsed time timer keeping the best try.
43 * On POSIX platforms we use clock_gettime with CLOCK_PROCESS_CPUTIME_ID.
44 * On Windows we use QueryPerformanceCounter
46 * Important: on linux, you must link with -lrt
54 #if defined(_WIN32) || defined(__CYGWIN__)
56 QueryPerformanceFrequency(&freq
);
57 m_frequency
= (double)freq
.QuadPart
;
72 m_starts
[CPU_TIMER
] = getCpuTime();
73 m_starts
[REAL_TIMER
] = getRealTime();
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
);
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));
91 /** Return the elapsed time in seconds between the last start/stop pair
93 inline double value(int TIMER
= CPU_TIMER
) const
95 return m_times
[TIMER
];
98 /** Return the best elapsed time in seconds
100 inline double best(int TIMER
= CPU_TIMER
) const
102 return m_bests
[TIMER
];
105 /** Return the worst elapsed time in seconds
107 inline double worst(int TIMER
= CPU_TIMER
) const
109 return m_worsts
[TIMER
];
112 /** Return the total elapsed time in seconds.
114 inline double total(int TIMER
= CPU_TIMER
) const
116 return m_totals
[TIMER
];
119 inline double getCpuTime() const
122 LARGE_INTEGER query_ticks
;
123 QueryPerformanceCounter(&query_ticks
);
124 return query_ticks
.QuadPart
/m_frequency
;
126 return double(mach_absolute_time())*1e-9;
129 clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &ts
);
130 return double(ts
.tv_sec
) + 1e-9 * double(ts
.tv_nsec
);
134 inline double getRealTime() const
139 return (double)st
.wSecond
+ 1.e
-3 * (double)st
.wMilliseconds
;
141 return double(mach_absolute_time())*1e-9;
144 clock_gettime(CLOCK_REALTIME
, &ts
);
145 return double(ts
.tv_sec
) + 1e-9 * double(ts
.tv_nsec
);
150 #if defined(_WIN32) || defined(__CYGWIN__)
160 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
163 #define BENCH(TIMER,TRIES,REP,CODE) { \
165 for(int uglyvarname1=0; uglyvarname1<TRIES; ++uglyvarname1){ \
167 for(int uglyvarname2=0; uglyvarname2<REP; ++uglyvarname2){ \
176 // clean #defined tokens
177 #ifdef EIGEN_BT_UNDEF_NOMINMAX
178 # undef EIGEN_BT_UNDEF_NOMINMAX
182 #ifdef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
183 # undef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
184 # undef WIN32_LEAN_AND_MEAN
187 #endif // EIGEN_BENCH_TIMERR_H