timers.h
Go to the documentation of this file.
1 
17 #ifndef ARUCO_TIMERS_H
18 #define ARUCO_TIMERS_H
19 
20 
21 #include <chrono>
22 #include <string>
23 #include <vector>
24 #include <iostream>
25 #include "aruco_export.h"
26 namespace aruco
27 {
28 
29 // timer
30 struct ScopeTimer
31 {
32  std::chrono::high_resolution_clock::time_point begin, end;
33 
34  std::string name;
35  bool use;
36  enum SCALE
37  {
41  };
43  ScopeTimer(std::string name_, bool use_ = true, SCALE _sc = MSEC)
44  {
45 #ifdef USE_TIMERS
46  name = name_;
47  use = use_;
48  sc = _sc;
49  begin = std::chrono::high_resolution_clock::now();
50 #else
51  (void)name_;
52  (void)use_;
53  (void)_sc;
54 
55 #endif
56  }
58  {
59 #ifdef USE_TIMERS
60  if (use)
61  {
62  end = std::chrono::high_resolution_clock::now();
63  double fact = 1;
64  std::string str;
65  switch (sc)
66  {
67  case NSEC:
68  fact = 1;
69  str = "ns";
70  break;
71  case MSEC:
72  fact = 1e6;
73  str = "ms";
74  break;
75  case SEC:
76  fact = 1e9;
77  str = "s";
78  break;
79  };
80 
81  std::cout << "Time(" << name << ")= "
82  << double(
83  std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count()) /
84  fact
85  << str << std::endl;
86  ;
87  }
88 #endif
89  }
90 };
91 
93 {
94  enum SCALE
95  {
99  };
101  std::vector<std::chrono::high_resolution_clock::time_point> vtimes;
102  std::vector<std::string> names;
103  std::string _name;
104 
105  ScopedTimerEvents(std::string name = "", bool start = true, SCALE _sc = MSEC)
106  {
107 #ifdef USE_TIMERS
108  if (start)
109  add("start");
110  sc = _sc;
111  _name = name;
112 #else
113  (void)name;
114  (void)start;
115  (void)_sc;
116 #endif
117  }
118 
119  void add(std::string name)
120  {
121 #ifdef USE_TIMERS
122  vtimes.push_back(std::chrono::high_resolution_clock::now());
123  names.push_back(name);
124 #else
125  (void)name;
126 #endif
127  }
128  void addspaces(std::vector<std::string> &str)
129  {
130  // get max size
131  size_t m = 0;
132  for (auto &s : str)
133  m = std::max(size_t(s.size()), m);
134  for (auto &s : str)
135  {
136  while (s.size() < m)
137  s.push_back(' ');
138  }
139  }
140 
142  {
143 #ifdef USE_TIMERS
144  double fact = 1;
145  std::string str;
146  switch (sc)
147  {
148  case NSEC:
149  fact = 1;
150  str = "ns";
151  break;
152  case MSEC:
153  fact = 1e6;
154  str = "ms";
155  break;
156  case SEC:
157  fact = 1e9;
158  str = "s";
159  break;
160  };
161 
162  add("total");
163  addspaces(names);
164  for (size_t i = 1; i < vtimes.size(); i++)
165  {
166  std::cout << "Time(" << _name << "|" << names[i] << " ):"
167  << double(std::chrono::duration_cast<std::chrono::nanoseconds>(
168  vtimes[i] - vtimes[i - 1])
169  .count()) /
170  fact
171  << str << " "
172  << double(std::chrono::duration_cast<std::chrono::nanoseconds>(vtimes[i] -
173  vtimes[0])
174  .count()) /
175  fact
176  << str /*<<"\t"<< vtimes[i].time_since_epoch().count()*/ << std::endl;
177  }
178 #endif
179  }
180 };
181 
183 {
184  enum SCALE
185  {
188  SEC
189  };
190 
191  std::chrono::high_resolution_clock::time_point _s;
192  double sum = 0, n = 0;
193  std::string _name;
195  {
196  }
197 
198  Timer(std::string name) : _name(name)
199  {
200  }
201  void setName(std::string name)
202  {
203  _name = name;
204  }
205  void start()
206  {
207  _s = std::chrono::high_resolution_clock::now();
208  }
209  void end()
210  {
211  auto e = std::chrono::high_resolution_clock::now();
212  sum += double(std::chrono::duration_cast<std::chrono::nanoseconds>(e - _s).count());
213  n++;
214  }
215 
216  void print(SCALE sc = MSEC)
217  {
218 #ifdef USE_TIMERS
219  double fact = 1;
220  std::string str;
221  switch (sc)
222  {
223  case NSEC:
224  fact = 1;
225  str = "ns";
226  break;
227  case MSEC:
228  fact = 1e6;
229  str = "ms";
230  break;
231  case SEC:
232  fact = 1e9;
233  str = "s";
234  break;
235  };
236  std::cout << "Time(" << _name << ")= " << (sum / n) / fact << str << std::endl;
237 #else
238  (void)sc;
239 
240 #endif
241  }
242 
243  double getAverage(SCALE sc = MSEC) const
244  {
245  double fact = 1;
246  switch (sc)
247  {
248  case NSEC:
249  fact = 1;
250  break;
251  case MSEC:
252  fact = 1e6;
253  break;
254  case SEC:
255  fact = 1e9;
256  break;
257  };
258  return (sum / n) / fact;
259  }
260 };
261 inline std::string __pf_aruco_methodName(std::string prettyFunction)
262 {
263  std::string res;
264  res.reserve(prettyFunction.size());
265  bool spaceFound = false;
266  for (auto c : prettyFunction)
267  {
268  if (c == ' ' && !spaceFound)
269  spaceFound = true;
270  else if (c != '(' && spaceFound)
271  res.push_back(c);
272  else if (c == '(' && spaceFound)
273  break;
274  }
275  return res;
276 }
277 #ifdef USE_TIMERS
278 
279 #define __ARUCO_ADDTIMER__ \
280  ScopedTimerEvents XTIMER_X(__pf_aruco_methodName(__PRETTY_FUNCTION__));
281 #define __ARUCO_TIMER_EVENT__(Y) XTIMER_X.add(Y);
282 #else
283 #define __ARUCO_ADDTIMER__
284 #define __ARUCO_TIMER_EVENT__(Y)
285 #endif
286 } // namespace aruco
287 
288 
289 #endif
aruco::Timer::Timer
Timer(std::string name)
Definition: timers.h:198
aruco::Timer::end
void end()
Definition: timers.h:209
aruco::Timer::MSEC
@ MSEC
Definition: timers.h:187
aruco::ScopeTimer::SCALE
SCALE
Definition: timers.h:36
aruco::ScopedTimerEvents::SEC
@ SEC
Definition: timers.h:98
aruco::ScopedTimerEvents::addspaces
void addspaces(std::vector< std::string > &str)
Definition: timers.h:128
aruco::Timer::print
void print(SCALE sc=MSEC)
Definition: timers.h:216
aruco::ScopedTimerEvents::SCALE
SCALE
Definition: timers.h:94
aruco::Timer::start
void start()
Definition: timers.h:205
aruco::Timer::_s
std::chrono::high_resolution_clock::time_point _s
Definition: timers.h:191
aruco::ScopeTimer::sc
SCALE sc
Definition: timers.h:42
aruco::Timer::NSEC
@ NSEC
Definition: timers.h:186
aruco::__pf_aruco_methodName
std::string __pf_aruco_methodName(std::string prettyFunction)
Definition: timers.h:261
aruco::ScopeTimer
Definition: timers.h:30
aruco::ScopedTimerEvents::_name
std::string _name
Definition: timers.h:103
aruco::ScopeTimer::begin
std::chrono::high_resolution_clock::time_point begin
Definition: timers.h:32
aruco::Timer::setName
void setName(std::string name)
Definition: timers.h:201
aruco::ScopeTimer::end
std::chrono::high_resolution_clock::time_point end
Definition: timers.h:32
aruco::ScopedTimerEvents::names
std::vector< std::string > names
Definition: timers.h:102
aruco_export.h
aruco::Timer::getAverage
double getAverage(SCALE sc=MSEC) const
Definition: timers.h:243
aruco::ScopeTimer::use
bool use
Definition: timers.h:35
aruco::Timer
Definition: timers.h:182
aruco::ScopeTimer::ScopeTimer
ScopeTimer(std::string name_, bool use_=true, SCALE _sc=MSEC)
Definition: timers.h:43
aruco::Timer::SCALE
SCALE
Definition: timers.h:184
aruco::ScopeTimer::name
std::string name
Definition: timers.h:34
aruco::Timer::_name
std::string _name
Definition: timers.h:193
aruco::ScopedTimerEvents::ScopedTimerEvents
ScopedTimerEvents(std::string name="", bool start=true, SCALE _sc=MSEC)
Definition: timers.h:105
aruco::ScopeTimer::~ScopeTimer
~ScopeTimer()
Definition: timers.h:57
aruco::ScopedTimerEvents
Definition: timers.h:92
ARUCO_EXPORT
#define ARUCO_EXPORT
Definition: aruco_export.h:30
aruco
Definition: cameraparameters.h:24
aruco::Timer::Timer
Timer()
Definition: timers.h:194
aruco::ScopedTimerEvents::add
void add(std::string name)
Definition: timers.h:119
aruco::ScopeTimer::SEC
@ SEC
Definition: timers.h:40
aruco::ScopeTimer::MSEC
@ MSEC
Definition: timers.h:39
aruco::ScopedTimerEvents::~ScopedTimerEvents
~ScopedTimerEvents()
Definition: timers.h:141
aruco::ScopedTimerEvents::vtimes
std::vector< std::chrono::high_resolution_clock::time_point > vtimes
Definition: timers.h:101
aruco::ScopeTimer::NSEC
@ NSEC
Definition: timers.h:38
aruco::ScopedTimerEvents::MSEC
@ MSEC
Definition: timers.h:97
aruco::ScopedTimerEvents::NSEC
@ NSEC
Definition: timers.h:96
aruco::ScopedTimerEvents::sc
SCALE sc
Definition: timers.h:100


aruco
Author(s): Rafael Muñoz Salinas , Bence Magyar
autogenerated on Sat Sep 23 2023 02:26:45