crocoddyl  1.8.1
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 // Uncomment the following line to activate the profiler
38 //#define PROFILER_ACTIVE
39 
40 #ifdef PROFILER_ACTIVE
41 #define START_PROFILER(name) getProfiler().start(name)
42 #define STOP_PROFILER(name) getProfiler().stop(name)
43 #else
44 #define START_PROFILER(name)
45 #define STOP_PROFILER(name)
46 #endif
47 
48 #define STOP_WATCH_MAX_NAME_LENGTH 60
49 #define STOP_WATCH_TIME_WIDTH 10
50 
51 namespace crocoddyl {
52 
53 // Generic stopwatch exception class
55  public:
56  StopwatchException(std::string error) : error(error) {}
57  std::string error;
58 };
59 
60 enum StopwatchMode {
61  NONE = 0, // Clock is not initialized
62  CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
63  REAL_TIME = 2 // Clock calculates time by asking the operating system how
64  // much real time passed
65 };
66 
158 class Stopwatch {
159  public:
161  Stopwatch(StopwatchMode _mode = NONE);
162 
164  ~Stopwatch();
165 
167  bool performance_exists(std::string perf_name);
168 
170  void set_mode(StopwatchMode mode);
171 
173  void start(const std::string &perf_name);
174 
176  void stop(const std::string &perf_name);
177 
179  void pause(const std::string &perf_name);
180 
182  void reset(const std::string &perf_name);
183 
185  void reset_all();
186 
188  void report(const std::string &perf_name, int precision = 2, std::ostream &output = std::cout);
189 
191  void report_all(int precision = 2, std::ostream &output = std::cout);
192 
194  long double get_total_time(const std::string &perf_name);
195 
197  long double get_average_time(const std::string &perf_name);
198 
200  long double get_min_time(const std::string &perf_name);
201 
203  long double get_max_time(const std::string &perf_name);
204 
206  long double get_last_time(const std::string &perf_name);
207 
211  long double get_time_so_far(const std::string &perf_name);
212 
215  void turn_off();
216 
218  void turn_on();
219 
221  long double take_time();
222 
223  protected:
227  : clock_start(0), total_time(0), min_time(0), max_time(0), last_time(0), paused(false), stops(0) {}
228 
229  long double clock_start;
230  long double total_time;
231  long double min_time;
232  long double max_time;
233  long double last_time;
234  bool paused;
235  int stops;
236  };
237 
238  bool active;
239  StopwatchMode mode;
240  std::map<std::string, PerformanceData> *records_of;
241 };
242 
243 Stopwatch &getProfiler();
244 
245 } // namespace crocoddyl
246 
247 #ifndef WIN32
248 #pragma GCC visibility pop
249 #endif
250 
251 #endif
long double clock_start
Start time.
Definition: stop-watch.hpp:229
Struct to hold the performance data.
Definition: stop-watch.hpp:225
int stops
How many cycles have been this stopwatch executed?
Definition: stop-watch.hpp:235
long double max_time
Maximum time.
Definition: stop-watch.hpp:232
long double min_time
Minimum time.
Definition: stop-watch.hpp:231
long double last_time
Last time.
Definition: stop-watch.hpp:233
long double total_time
Cumulative total time.
Definition: stop-watch.hpp:230
StopwatchMode mode
Time taking mode.
Definition: stop-watch.hpp:239
bool active
Flag to hold the clock&#39;s status.
Definition: stop-watch.hpp:238
std::map< std::string, PerformanceData > * records_of
Dynamic collection of performance data.
Definition: stop-watch.hpp:240
bool paused
Tells if this performance has been paused, only for internal use.
Definition: stop-watch.hpp:234
A class representing a stopwatch.
Definition: stop-watch.hpp:158