OpenGM  2.3.x
Discrete Graphical Model Library
timer.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_TIMER_HXX
3 #define OPENGM_TIMER_HXX
4 
5 #include <stdexcept>
6 
7 # if (defined(_OPENGM_TIMER_MACH__) || defined(__APPLE__))
8 # define OPENGM_TIMER_MAC
9 # elif (defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_WIN64))
10 # define OPENGM_TIMER_WINDOWS
11 # ifndef WIN32_LEAN_AND_MEAN
12 # define WIN32_LEAN_AND_MEAN
13 # endif
14 # endif
15 
16 # if defined(OPENGM_TIMER_MAC)
17 # include <mach/mach_time.h>
18 # elif defined(OPENGM_TIMER_WINDOWS)
19 # include <windows.h>
20 # undef min
21 # undef max
22 # else
23 # include <time.h>
24 # endif
25 
26 namespace opengm {
27 
29 class Timer {
30 public:
31  // construction
32  Timer();
33 
34  // query
35  double elapsedTime() const;
36 
37  // manipulation
38  void tic();
39  void toc();
40  void reset();
41 
42 private:
43  #if defined(OPENGM_TIMER_MAC)
44  typedef uint64_t TimerT;
45  typedef double TimerC;
46  #elif defined(OPENGM_TIMER_WINDOWS)
47  typedef LONGLONG TimerT;
48  typedef LARGE_INTEGER TimerC;
49  #else
50  typedef double TimerT;
51  typedef timespec TimerC;
52  #endif
53 
54  TimerT start_;
55 #if !defined(OPENGM_TIMER_MAC)
56  TimerC ts_;
57 #endif
58  double duration_;
59  double conversionFactor_;
60  double elapsedTime_;
61 };
62 
64 template<class FUNCTOR>
65 class Timing {
66 public:
67  typedef FUNCTOR Functor;
68 
69  Timing(Functor, const size_t = 1);
70  const std::vector<double>& times() const;
71 
72 private:
73  Functor functor_;
74  std::vector<double> times_;
75 };
76 
77 inline Timer::Timer()
78 : start_(0), duration_(0), elapsedTime_(0)
79 {
80  #if defined(OPENGM_TIMER_MAC)
81  mach_timebase_info_data_t info;
82  mach_timebase_info(&info);
83  conversionFactor_ = (static_cast<double>(info.numer))/
84  (static_cast<double>(info.denom));
85  conversionFactor_ = conversionFactor_*1.0e-9;
86  #elif defined(OPENGM_TIMER_WINDOWS)
87  TimerC freq;
88  QueryPerformanceFrequency(&freq);
89  conversionFactor_ = 1.0/(static_cast<double>(freq.QuadPart));
90  #else
91  conversionFactor_ = 1.0;
92  #endif
93  reset();
94 }
95 
96 inline void Timer::tic() {
97  #if defined(OPENGM_TIMER_MAC)
98  start_ = mach_absolute_time();
99  #elif defined(OPENGM_TIMER_WINDOWS)
100  QueryPerformanceCounter(&ts_);
101  start_ = ts_.QuadPart;
102  #else
103  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_);
104  start_ = static_cast<double>(ts_.tv_sec) + 1.0e-9 *
105  static_cast<double>(ts_.tv_nsec);
106  #endif
107 }
108 
109 inline void Timer::toc() {
110  #if defined(OPENGM_TIMER_MAC)
111  duration_ = static_cast<double>(mach_absolute_time() - start_);
112  #elif defined(OPENGM_TIMER_WINDOWS)
113  LARGE_INTEGER qpc_t; // ???
114  QueryPerformanceCounter(&qpc_t);
115  duration_ = static_cast<double>(qpc_t.QuadPart - start_);
116  #else
117  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_);
118  duration_ = (static_cast<double>(ts_.tv_sec) + 1.0e-9 *
119  static_cast<double>(ts_.tv_nsec)) - start_;
120  #endif
121  elapsedTime_ = duration_*conversionFactor_;
122 }
123 
124 inline void Timer::reset() {
125  start_ = 0;
126  duration_ = 0;
127  elapsedTime_ = 0;
128 }
129 
130 inline double Timer::elapsedTime() const {
131  return elapsedTime_;
132 }
133 
134 template<class FUNCTOR>
136  FUNCTOR functor,
137  const size_t repetitions
138 )
139 : functor_(functor),
140  times_(std::vector<double>())
141 {
142  if(repetitions < 1) {
143  throw std::runtime_error("The number of repetition must be at least 1.");
144  }
145  for(size_t j=0; j<repetitions; ++j) {
146  opengm::Timer timer;
147  timer.tic();
148  functor_();
149  timer.toc();
150  times_.push_back(timer.elapsedTime());
151  }
152 }
153 
154 template<class FUNCTOR>
155 inline const std::vector<double>&
157  return times_;
158 }
159 
160 } // namespace opengm
161 
162 #if defined(OPENGM_TIMER_WINDOWS)
163 # undef WIN32_LEAN_AND_MEAN
164 #endif
165 
166 #endif // OPENGM_TIMER_HXX
The OpenGM namespace.
Definition: config.hxx:43
void reset()
Definition: timer.hxx:124
Timing(Functor, const size_t=1)
Definition: timer.hxx:135
void toc()
Definition: timer.hxx:109
STL namespace.
Platform-independent runtime measurements.
Definition: timer.hxx:29
const std::vector< double > & times() const
Definition: timer.hxx:156
Platform-independent runtime measurements of functors.
Definition: timer.hxx:65
double elapsedTime() const
Definition: timer.hxx:130
void tic()
Definition: timer.hxx:96
FUNCTOR Functor
Definition: timer.hxx:67