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  asm volatile("" : : "g"(p) : "memory");
32 }
33 
34 static void clobber() {
35  asm volatile("" : : : "memory");
36 }
37 
38 #include <Eigen/Core>
39 
40 namespace Eigen
41 {
42 
43 enum {
44  CPU_TIMER = 0,
46 };
47 
56 {
57 public:
58 
60  {
61 #if defined(_WIN32) || defined(__CYGWIN__)
62  LARGE_INTEGER freq;
63  QueryPerformanceFrequency(&freq);
64  m_frequency = (double)freq.QuadPart;
65 #endif
66  reset();
67  }
68 
70 
71  inline void reset()
72  {
73  m_bests.fill(1e9);
74  m_worsts.fill(0);
75  m_totals.setZero();
76  }
77  inline void start()
78  {
81  }
82  inline void stop()
83  {
86  #if EIGEN_VERSION_AT_LEAST(2,90,0)
87  m_bests = m_bests.cwiseMin(m_times);
88  m_worsts = m_worsts.cwiseMax(m_times);
89  #else
90  m_bests(0) = std::min(m_bests(0),m_times(0));
91  m_bests(1) = std::min(m_bests(1),m_times(1));
92  m_worsts(0) = std::max(m_worsts(0),m_times(0));
93  m_worsts(1) = std::max(m_worsts(1),m_times(1));
94  #endif
95  m_totals += m_times;
96  }
97 
100  inline double value(int TIMER = CPU_TIMER) const
101  {
102  return m_times[TIMER];
103  }
104 
107  inline double best(int TIMER = CPU_TIMER) const
108  {
109  return m_bests[TIMER];
110  }
111 
114  inline double worst(int TIMER = CPU_TIMER) const
115  {
116  return m_worsts[TIMER];
117  }
118 
121  inline double total(int TIMER = CPU_TIMER) const
122  {
123  return m_totals[TIMER];
124  }
125 
126  inline double getCpuTime() const
127  {
128 #ifdef _WIN32
129  LARGE_INTEGER query_ticks;
130  QueryPerformanceCounter(&query_ticks);
131  return query_ticks.QuadPart/m_frequency;
132 #elif __APPLE__
133  return double(mach_absolute_time())*1e-9;
134 #else
135  timespec ts;
136  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
137  return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
138 #endif
139  }
140 
141  inline double getRealTime() const
142  {
143 #ifdef _WIN32
144  SYSTEMTIME st;
145  GetSystemTime(&st);
146  return (double)st.wSecond + 1.e-3 * (double)st.wMilliseconds;
147 #elif __APPLE__
148  return double(mach_absolute_time())*1e-9;
149 #else
150  timespec ts;
151  clock_gettime(CLOCK_REALTIME, &ts);
152  return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
153 #endif
154  }
155 
156 protected:
157 #if defined(_WIN32) || defined(__CYGWIN__)
158  double m_frequency;
159 #endif
160  Vector2d m_starts;
161  Vector2d m_times;
162  Vector2d m_bests;
163  Vector2d m_worsts;
164  Vector2d m_totals;
165 
166 public:
168 };
169 
170 #define BENCH(TIMER,TRIES,REP,CODE) { \
171  TIMER.reset(); \
172  for(int uglyvarname1=0; uglyvarname1<TRIES; ++uglyvarname1){ \
173  TIMER.start(); \
174  for(int uglyvarname2=0; uglyvarname2<REP; ++uglyvarname2){ \
175  CODE; \
176  } \
177  TIMER.stop(); \
178  clobber(); \
179  } \
180  }
181 
182 }
183 
184 // clean #defined tokens
185 #ifdef EIGEN_BT_UNDEF_NOMINMAX
186 # undef EIGEN_BT_UNDEF_NOMINMAX
187 # undef NOMINMAX
188 #endif
189 
190 #ifdef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
191 # undef EIGEN_BT_UNDEF_WIN32_LEAN_AND_MEAN
192 # undef WIN32_LEAN_AND_MEAN
193 #endif
194 
195 #endif // EIGEN_BENCH_TIMERR_H
#define max(a, b)
Definition: datatypes.h:20
#define min(a, b)
Definition: datatypes.h:19
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:690
double getRealTime() const
Definition: BenchTimer.h:141
static void clobber()
Definition: BenchTimer.h:34
double worst(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:114
double getCpuTime() const
Definition: BenchTimer.h:126
Vector2d m_starts
Definition: BenchTimer.h:160
Vector2d m_times
Definition: BenchTimer.h:161
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Vector2d m_totals
Definition: BenchTimer.h:164
double total(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:121
double value(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:100
double best(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:107
float * p
static void escape(void *p)
Definition: BenchTimer.h:30
Vector2d m_worsts
Definition: BenchTimer.h:163
Vector2d m_bests
Definition: BenchTimer.h:162


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:42