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 #ifndef __invdyn_stopwatch_H__
28 #define __invdyn_stopwatch_H__
29 
30 #include "tsid/utils/Stdafx.hh"
31 
32 #ifndef WIN32
33 /* The classes below are exported */
34 #pragma GCC visibility push(default)
35 #endif
36 
37 // #define START_PROFILER(name)
38 // #define STOP_PROFILER(name)
39 #define START_PROFILER(name) getProfiler().start(name)
40 #define STOP_PROFILER(name) getProfiler().stop(name)
41 
42 #define STOP_WATCH_MAX_NAME_LENGTH 60
43 #define STOP_WATCH_TIME_WIDTH 10
44 
45 // Generic stopwatch exception class
47  public:
48  StopwatchException(std::string error) : error(error) {}
49  std::string error;
50 };
51 
53  NONE = 0, // Clock is not initialized
54  CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
55  REAL_TIME = 2 // Clock calculates time by asking the operating system how
56  // much real time passed
57 };
58 
150 class Stopwatch {
151  public:
153  Stopwatch(StopwatchMode _mode = NONE);
154 
156  ~Stopwatch();
157 
159  bool performance_exists(std::string perf_name);
160 
163 
165  void start(std::string perf_name);
166 
168  void stop(std::string perf_name);
169 
171  void pause(std::string perf_name);
172 
174  void reset(std::string perf_name);
175 
177  void reset_all();
178 
180  void report(std::string perf_name, int precision = 2,
181  std::ostream& output = std::cout);
182 
184  void report_all(int precision = 2, std::ostream& output = std::cout);
185 
187  long double get_total_time(std::string perf_name);
188 
190  long double get_average_time(std::string perf_name);
191 
193  long double get_min_time(std::string perf_name);
194 
196  long double get_max_time(std::string perf_name);
197 
199  long double get_last_time(std::string perf_name);
200 
203  long double get_time_so_far(std::string perf_name);
204 
207  void turn_off();
208 
210  void turn_on();
211 
213  long double take_time();
214 
215  protected:
219  : clock_start(0),
220  total_time(0),
221  min_time(0),
222  max_time(0),
223  last_time(0),
224  paused(false),
225  stops(0) {}
226 
228  long double clock_start;
229 
231  long double total_time;
232 
234  long double min_time;
235 
237  long double max_time;
238 
240  long double last_time;
241 
243  bool paused;
244 
246  int stops;
247  };
248 
250  bool active;
251 
254 
257  std::map<std::string, PerformanceData>* records_of;
258 };
259 
261 
262 #ifndef WIN32
263 #pragma GCC visibility pop
264 #endif
265 
266 #endif
Stopwatch::stop
void stop(std::string perf_name)
Definition: stop-watch.cpp:120
Stopwatch::get_average_time
long double get_average_time(std::string perf_name)
Definition: stop-watch.cpp:286
StopwatchException
Definition: stop-watch.hpp:46
Stopwatch::get_max_time
long double get_max_time(std::string perf_name)
Definition: stop-watch.cpp:306
Stopwatch::get_last_time
long double get_last_time(std::string perf_name)
Definition: stop-watch.cpp:316
Stopwatch::PerformanceData::max_time
long double max_time
Definition: stop-watch.hpp:237
Stopwatch::~Stopwatch
~Stopwatch()
Definition: stop-watch.cpp:52
Stopwatch::get_min_time
long double get_min_time(std::string perf_name)
Definition: stop-watch.cpp:296
Stopwatch::PerformanceData
Definition: stop-watch.hpp:217
REAL_TIME
@ REAL_TIME
Definition: stop-watch.hpp:55
Stopwatch::active
bool active
Definition: stop-watch.hpp:250
Stopwatch::performance_exists
bool performance_exists(std::string perf_name)
Definition: stop-watch.cpp:56
Stopwatch::PerformanceData::paused
bool paused
Definition: stop-watch.hpp:243
Stopwatch::PerformanceData::PerformanceData
PerformanceData()
Definition: stop-watch.hpp:218
Stopwatch
A class representing a stopwatch.
Definition: stop-watch.hpp:150
Stopwatch::take_time
long double take_time()
Definition: stop-watch.cpp:60
CPU_TIME
@ CPU_TIME
Definition: stop-watch.hpp:54
getProfiler
Stopwatch & getProfiler()
Definition: stop-watch.cpp:43
Stopwatch::get_time_so_far
long double get_time_so_far(std::string perf_name)
Definition: stop-watch.cpp:263
Stopwatch::Stopwatch
Stopwatch(StopwatchMode _mode=NONE)
Definition: stop-watch.cpp:48
Stopwatch::set_mode
void set_mode(StopwatchMode mode)
Definition: stop-watch.cpp:54
Stopwatch::PerformanceData::stops
int stops
Definition: stop-watch.hpp:246
Stopwatch::PerformanceData::last_time
long double last_time
Definition: stop-watch.hpp:240
Stopwatch::pause
void pause(std::string perf_name)
Definition: stop-watch.cpp:151
Stopwatch::turn_off
void turn_off()
Definition: stop-watch.cpp:229
Stopwatch::report
void report(std::string perf_name, int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:234
Stopwatch::mode
StopwatchMode mode
Definition: stop-watch.hpp:253
Stopwatch::turn_on
void turn_on()
Definition: stop-watch.cpp:224
demo_quadruped.precision
precision
Definition: demo_quadruped.py:18
Stopwatch::records_of
std::map< std::string, PerformanceData > * records_of
Definition: stop-watch.hpp:257
Stopwatch::PerformanceData::clock_start
long double clock_start
Definition: stop-watch.hpp:228
StopwatchMode
StopwatchMode
Definition: stop-watch.hpp:52
Stopwatch::get_total_time
long double get_total_time(std::string perf_name)
Definition: stop-watch.cpp:276
Stdafx.hh
StopwatchException::error
std::string error
Definition: stop-watch.hpp:49
Stopwatch::reset_all
void reset_all()
Definition: stop-watch.cpp:172
StopwatchException::StopwatchException
StopwatchException(std::string error)
Definition: stop-watch.hpp:48
Stopwatch::PerformanceData::min_time
long double min_time
Definition: stop-watch.hpp:234
Stopwatch::start
void start(std::string perf_name)
Definition: stop-watch.cpp:102
Stopwatch::reset
void reset(std::string perf_name)
Definition: stop-watch.cpp:206
Stopwatch::report_all
void report_all(int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:182
NONE
@ NONE
Definition: stop-watch.hpp:53
Stopwatch::PerformanceData::total_time
long double total_time
Definition: stop-watch.hpp:231


tsid
Author(s): Andrea Del Prete, Justin Carpentier
autogenerated on Thu Apr 3 2025 02:47:16