crocoddyl  1.9.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
stop-watch.hpp
1 // Copyright (c) 2010-2013 Tommaso Urli (tommaso.urli@uniud.it; University of Udine)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 #ifndef CROCODDYL_CORE_UTILS_STOPWATCH_H_
25 #define CROCODDYL_CORE_UTILS_STOPWATCH_H_
26 
27 #include <iostream>
28 #include <map>
29 #include <ctime>
30 #include <sstream>
31 
32 #ifndef WIN32
33 /* The classes below are exported */
34 #pragma GCC visibility push(default)
35 #endif
36 
37 #define START_PROFILER(name) getProfiler().profiler_status() ? getProfiler().start(name) : void()
38 #define STOP_PROFILER(name) getProfiler().profiler_status() ? getProfiler().stop(name) : void()
39 
40 #define STOP_WATCH_MAX_NAME_LENGTH 60
41 #define STOP_WATCH_TIME_WIDTH 10
42 
43 namespace crocoddyl {
44 
45 // Generic stopwatch exception class
47  public:
48  StopwatchException(std::string error) : error(error) {}
49  std::string error;
50 };
51 
52 enum StopwatchMode {
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  void enable_profiler();
160 
162  void disable_profiler();
163 
165  bool profiler_status();
166 
168  bool performance_exists(std::string perf_name);
169 
171  void set_mode(StopwatchMode mode);
172 
174  void start(const std::string &perf_name);
175 
177  void stop(const std::string &perf_name);
178 
180  void pause(const std::string &perf_name);
181 
183  void reset(const std::string &perf_name);
184 
186  void reset_all();
187 
189  void report(const std::string &perf_name, int precision = 2, std::ostream &output = std::cout);
190 
192  void report_all(int precision = 2, std::ostream &output = std::cout);
193 
195  long double get_total_time(const std::string &perf_name);
196 
198  long double get_average_time(const std::string &perf_name);
199 
201  long double get_min_time(const std::string &perf_name);
202 
204  long double get_max_time(const std::string &perf_name);
205 
207  long double get_last_time(const std::string &perf_name);
208 
212  long double get_time_so_far(const std::string &perf_name);
213 
216  void turn_off();
217 
219  void turn_on();
220 
222  long double take_time();
223 
224  protected:
228  : clock_start(0), total_time(0), min_time(0), max_time(0), last_time(0), paused(false), stops(0) {}
229 
230  long double clock_start;
231  long double total_time;
232  long double min_time;
233  long double max_time;
234  long double last_time;
235  bool paused;
236  int stops;
237  };
238 
239  bool active;
240  StopwatchMode mode;
241  std::map<std::string, PerformanceData> *records_of;
243 };
244 
245 Stopwatch &getProfiler();
246 
247 } // namespace crocoddyl
248 
249 #ifndef WIN32
250 #pragma GCC visibility pop
251 #endif
252 
253 #endif
long double clock_start
Start time.
Definition: stop-watch.hpp:230
Struct to hold the performance data.
Definition: stop-watch.hpp:226
int stops
How many cycles have been this stopwatch executed?
Definition: stop-watch.hpp:236
long double max_time
Maximum time.
Definition: stop-watch.hpp:233
bool profiler_active
Indicates if the profiler is enabled.
Definition: stop-watch.hpp:242
long double min_time
Minimum time.
Definition: stop-watch.hpp:232
long double last_time
Last time.
Definition: stop-watch.hpp:234
long double total_time
Cumulative total time.
Definition: stop-watch.hpp:231
StopwatchMode mode
Time taking mode.
Definition: stop-watch.hpp:240
bool active
Flag to hold the clock&#39;s status.
Definition: stop-watch.hpp:239
std::map< std::string, PerformanceData > * records_of
Dynamic collection of performance data.
Definition: stop-watch.hpp:241
bool paused
Tells if this performance has been paused, only for internal use.
Definition: stop-watch.hpp:235
A class representing a stopwatch.
Definition: stop-watch.hpp:150