stop-watch.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2013 Tommaso Urli
3 
4 Tommaso Urli tommaso.urli@uniud.it University of Udine
5 
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13 
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 */
26 
27 
28 #ifndef __invdyn_stopwatch_H__
29 #define __invdyn_stopwatch_H__
30 
31 #include "tsid/utils/Stdafx.hh"
32 
33 #ifndef WIN32
34 /* The classes below are exported */
35 #pragma GCC visibility push(default)
36 #endif
37 
38 //#define START_PROFILER(name)
39 //#define STOP_PROFILER(name)
40 #define START_PROFILER(name) getProfiler().start(name)
41 #define STOP_PROFILER(name) getProfiler().stop(name)
42 
43 #define STOP_WATCH_MAX_NAME_LENGTH 60
44 #define STOP_WATCH_TIME_WIDTH 10
45 
46 // Generic stopwatch exception class
48 {
49 public:
50  StopwatchException(std::string error) : error(error) { }
51  std::string error;
52 };
53 
54 
56 {
57  NONE = 0, // Clock is not initialized
58  CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
59  REAL_TIME = 2 // Clock calculates time by asking the operating system how
60  // much real time passed
61 };
62 
63 
155 class Stopwatch {
156 public:
157 
160 
162  ~Stopwatch();
163 
165  bool performance_exists(std::string perf_name);
166 
168  void set_mode(StopwatchMode mode);
169 
171  void start(std::string perf_name);
172 
174  void stop(std::string perf_name);
175 
177  void pause(std::string perf_name);
178 
180  void reset(std::string perf_name);
181 
183  void reset_all();
184 
186  void report(std::string perf_name, int precision=2,
187  std::ostream& output = std::cout);
188 
190  void report_all(int precision=2, std::ostream& output = std::cout);
191 
193  long double get_total_time(std::string perf_name);
194 
196  long double get_average_time(std::string perf_name);
197 
199  long double get_min_time(std::string perf_name);
200 
202  long double get_max_time(std::string perf_name);
203 
205  long double get_last_time(std::string perf_name);
206 
209  long double get_time_so_far(std::string perf_name);
210 
213  void turn_off();
214 
216  void turn_on();
217 
219  long double take_time();
220 
221 protected:
222 
225 
227  clock_start(0),
228  total_time(0),
229  min_time(0),
230  max_time(0),
231  last_time(0),
232  paused(false),
233  stops(0) {
234  }
235 
237  long double clock_start;
238 
240  long double total_time;
241 
243  long double min_time;
244 
246  long double max_time;
247 
249  long double last_time;
250 
252  bool paused;
253 
255  int stops;
256  };
257 
259  bool active;
260 
263 
266  std::map<std::string, PerformanceData >* records_of;
267 
268 };
269 
271 
272 #ifndef WIN32
273 #pragma GCC visibility pop
274 #endif
275 
276 #endif
Definition: stop-watch.hpp:224
std::string error
Definition: stop-watch.hpp:51
PerformanceData()
Definition: stop-watch.hpp:226
Stopwatch & getProfiler()
Definition: stop-watch.cpp:44
long double last_time
Definition: stop-watch.hpp:249
long double min_time
Definition: stop-watch.hpp:243
Definition: stop-watch.hpp:47
A class representing a stopwatch.
Definition: stop-watch.hpp:155
Definition: stop-watch.hpp:57
StopwatchMode mode
Definition: stop-watch.hpp:262
bool paused
Definition: stop-watch.hpp:252
long double clock_start
Definition: stop-watch.hpp:237
StopwatchMode
Definition: stop-watch.hpp:55
long double total_time
Definition: stop-watch.hpp:240
int stops
Definition: stop-watch.hpp:255
std::map< std::string, PerformanceData > * records_of
Definition: stop-watch.hpp:266
bool active
Definition: stop-watch.hpp:259
Definition: stop-watch.hpp:59
StopwatchException(std::string error)
Definition: stop-watch.hpp:50
long double max_time
Definition: stop-watch.hpp:246
Definition: stop-watch.hpp:58