BenchTimer.h
Go to the documentation of this file.
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 
11 #ifndef EIGEN_BENCH_TIMERR_H
12 #define EIGEN_BENCH_TIMERR_H
13 
14 #if defined(_WIN32) || defined(__CYGWIN__)
15 #ifndef NOMINMAX
16 #define NOMINMAX
17 #define EIGEN_BT_UNDEF_NOMINMAX
18 #endif
19 #ifndef WIN32_LEAN_AND_MEAN
20 #define WIN32_LEAN_AND_MEAN
21 #define EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
22 #endif
23 #include <windows.h>
24 #elif defined(__APPLE__)
25 #include <mach/mach_time.h>
26 #else
27 #include <unistd.h>
28 #endif
29 
30 static void escape(void *p) {
31 #if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
32  asm volatile("" : : "g"(p) : "memory");
33 #endif
34 }
35 
36 static void clobber() {
37 #if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
38  asm volatile("" : : : "memory");
39 #endif
40 }
41 
42 #include <Eigen/Core>
43 
44 namespace Eigen {
45 
46 enum { CPU_TIMER = 0, REAL_TIMER = 1 };
47 
55 class BenchTimer {
56  public:
58 #if defined(_WIN32) || defined(__CYGWIN__)
59  LARGE_INTEGER freq;
60  QueryPerformanceFrequency(&freq);
61  m_frequency = (double)freq.QuadPart;
62 #endif
63  reset();
64  }
65 
67 
68  inline void reset() {
69  m_bests.fill(1e9);
70  m_worsts.fill(0);
71  m_totals.setZero();
72  }
73  inline void start() {
76  }
77  inline void stop() {
80 #if EIGEN_VERSION_AT_LEAST(2, 90, 0)
81  m_bests = m_bests.cwiseMin(m_times);
82  m_worsts = m_worsts.cwiseMax(m_times);
83 #else
84  m_bests(0) = std::min(m_bests(0), m_times(0));
85  m_bests(1) = std::min(m_bests(1), m_times(1));
86  m_worsts(0) = std::max(m_worsts(0), m_times(0));
87  m_worsts(1) = std::max(m_worsts(1), m_times(1));
88 #endif
89  m_totals += m_times;
90  }
91 
94  inline double value(int TIMER = CPU_TIMER) const { return m_times[TIMER]; }
95 
98  inline double best(int TIMER = CPU_TIMER) const { return m_bests[TIMER]; }
99 
102  inline double worst(int TIMER = CPU_TIMER) const { return m_worsts[TIMER]; }
103 
106  inline double total(int TIMER = CPU_TIMER) const { return m_totals[TIMER]; }
107 
108  inline double getCpuTime() const {
109 #ifdef _WIN32
110  LARGE_INTEGER query_ticks;
111  QueryPerformanceCounter(&query_ticks);
112  return query_ticks.QuadPart / m_frequency;
113 #elif __APPLE__
114  return double(mach_absolute_time()) * 1e-9;
115 #else
116  timespec ts;
117  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
118  return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
119 #endif
120  }
121 
122  inline double getRealTime() const {
123 #ifdef _WIN32
124  SYSTEMTIME st;
125  GetSystemTime(&st);
126  return (double)st.wSecond + 1.e-3 * (double)st.wMilliseconds;
127 #elif __APPLE__
128  return double(mach_absolute_time()) * 1e-9;
129 #else
130  timespec ts;
131  clock_gettime(CLOCK_REALTIME, &ts);
132  return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
133 #endif
134  }
135 
136  protected:
137 #if defined(_WIN32) || defined(__CYGWIN__)
138  double m_frequency;
139 #endif
140  Vector2d m_starts;
141  Vector2d m_times;
142  Vector2d m_bests;
143  Vector2d m_worsts;
144  Vector2d m_totals;
145 
146  public:
148 };
149 
150 #define BENCH(TIMER, TRIES, REP, CODE) \
151  { \
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 } // namespace Eigen
164 
165 // clean #defined tokens
166 #ifdef EIGEN_BT_UNDEF_NOMINMAX
167 #undef EIGEN_BT_UNDEF_NOMINMAX
168 #undef NOMINMAX
169 #endif
170 
171 #ifdef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
172 #undef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
173 #undef WIN32_LEAN_AND_MEAN
174 #endif
175 
176 #endif // EIGEN_BENCH_TIMERR_H
static void clobber()
Definition: BenchTimer.h:36
static void escape(void *p)
Definition: BenchTimer.h:30
Array< double, 1, 3 > e(1./3., 0.5, 2.)
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:879
float * p
Definition: Tutorial_Map_using.cpp:9
Definition: BenchTimer.h:55
BenchTimer()
Definition: BenchTimer.h:57
Vector2d m_totals
Definition: BenchTimer.h:144
double value(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:94
void reset()
Definition: BenchTimer.h:68
void stop()
Definition: BenchTimer.h:77
Vector2d m_times
Definition: BenchTimer.h:141
double getCpuTime() const
Definition: BenchTimer.h:108
Vector2d m_bests
Definition: BenchTimer.h:142
void start()
Definition: BenchTimer.h:73
Vector2d m_worsts
Definition: BenchTimer.h:143
~BenchTimer()
Definition: BenchTimer.h:66
Vector2d m_starts
Definition: BenchTimer.h:140
double worst(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:102
double best(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:98
double getRealTime() const
Definition: BenchTimer.h:122
double total(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:106
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
@ REAL_TIMER
Definition: BenchTimer.h:46
@ CPU_TIMER
Definition: BenchTimer.h:46