crocoddyl  1.7.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 "crocoddyl/core/utils/Stdafx.hh"
28 
29 #ifndef WIN32
30 /* The classes below are exported */
31 #pragma GCC visibility push(default)
32 #endif
33 
34 // Uncomment the following line to activate the profiler
35 //#define PROFILER_ACTIVE
36 
37 #ifdef PROFILER_ACTIVE
38 #define START_PROFILER(name) getProfiler().start(name)
39 #define STOP_PROFILER(name) getProfiler().stop(name)
40 #else
41 #define START_PROFILER(name)
42 #define STOP_PROFILER(name)
43 #endif
44 
45 #define STOP_WATCH_MAX_NAME_LENGTH 60
46 #define STOP_WATCH_TIME_WIDTH 10
47 
48 namespace crocoddyl {
49 
50 // Generic stopwatch exception class
52  public:
53  StopwatchException(std::string error) : error(error) {}
54  std::string error;
55 };
56 
57 enum StopwatchMode {
58  NONE = 0, // Clock is not initialized
59  CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
60  REAL_TIME = 2 // Clock calculates time by asking the operating system how
61  // much real time passed
62 };
63 
155 class Stopwatch {
156  public:
158  Stopwatch(StopwatchMode _mode = NONE);
159 
161  ~Stopwatch();
162 
164  bool performance_exists(std::string perf_name);
165 
167  void set_mode(StopwatchMode mode);
168 
170  void start(const std::string &perf_name);
171 
173  void stop(const std::string &perf_name);
174 
176  void pause(const std::string &perf_name);
177 
179  void reset(const std::string &perf_name);
180 
182  void reset_all();
183 
185  void report(const std::string &perf_name, int precision = 2, std::ostream &output = std::cout);
186 
188  void report_all(int precision = 2, std::ostream &output = std::cout);
189 
191  long double get_total_time(const std::string &perf_name);
192 
194  long double get_average_time(const std::string &perf_name);
195 
197  long double get_min_time(const std::string &perf_name);
198 
200  long double get_max_time(const std::string &perf_name);
201 
203  long double get_last_time(const std::string &perf_name);
204 
208  long double get_time_so_far(const std::string &perf_name);
209 
212  void turn_off();
213 
215  void turn_on();
216 
218  long double take_time();
219 
220  protected:
224  : clock_start(0), total_time(0), min_time(0), max_time(0), last_time(0), paused(false), stops(0) {}
225 
226  long double clock_start;
227  long double total_time;
228  long double min_time;
229  long double max_time;
230  long double last_time;
231  bool paused;
232  int stops;
233  };
234 
235  bool active;
236  StopwatchMode mode;
237  std::map<std::string, PerformanceData> *records_of;
238 };
239 
240 Stopwatch &getProfiler();
241 
242 } // namespace crocoddyl
243 
244 #ifndef WIN32
245 #pragma GCC visibility pop
246 #endif
247 
248 #endif
crocoddyl::Stopwatch::get_min_time
long double get_min_time(const std::string &perf_name)
Returns minimum execution time of a certain performance.
Definition: stop-watch.cpp:276
crocoddyl::Stopwatch::mode
StopwatchMode mode
Time taking mode.
Definition: stop-watch.hpp:236
crocoddyl::Stopwatch::performance_exists
bool performance_exists(std::string perf_name)
Tells if a performance with a certain ID exists.
Definition: stop-watch.cpp:55
crocoddyl::Stopwatch::records_of
std::map< std::string, PerformanceData > * records_of
Dynamic collection of performance data.
Definition: stop-watch.hpp:237
crocoddyl::Stopwatch::report
void report(const std::string &perf_name, int precision=2, std::ostream &output=std::cout)
Dump the data of a certain performance record.
Definition: stop-watch.cpp:225
crocoddyl::Stopwatch::reset_all
void reset_all()
Resets all the performance records.
Definition: stop-watch.cpp:166
crocoddyl::Stopwatch::get_time_so_far
long double get_time_so_far(const std::string &perf_name)
Return the time since the start of the last measurement of a given performance.
Definition: stop-watch.cpp:247
crocoddyl::StopwatchException
Definition: stop-watch.hpp:51
crocoddyl::Stopwatch::PerformanceData::total_time
long double total_time
Cumulative total time.
Definition: stop-watch.hpp:227
crocoddyl::Stopwatch::turn_on
void turn_on()
Turn on clock, restore clock operativity after a turn_off().
Definition: stop-watch.cpp:215
crocoddyl::Stopwatch::PerformanceData::min_time
long double min_time
Minimum time.
Definition: stop-watch.hpp:228
crocoddyl::Stopwatch::get_total_time
long double get_total_time(const std::string &perf_name)
Returns total execution time of a certain performance.
Definition: stop-watch.cpp:258
crocoddyl::Stopwatch::start
void start(const std::string &perf_name)
Start the stopwatch related to a certain piece of code.
Definition: stop-watch.cpp:99
crocoddyl::Stopwatch::take_time
long double take_time()
Take time, depends on mode.
Definition: stop-watch.cpp:57
crocoddyl::Stopwatch::PerformanceData
Struct to hold the performance data.
Definition: stop-watch.hpp:222
crocoddyl::Stopwatch::reset
void reset(const std::string &perf_name)
Reset a certain performance record.
Definition: stop-watch.cpp:198
crocoddyl::Stopwatch::~Stopwatch
~Stopwatch()
Destructor.
Definition: stop-watch.cpp:51
crocoddyl::Stopwatch::report_all
void report_all(int precision=2, std::ostream &output=std::cout)
Dump the data of all the performance records.
Definition: stop-watch.cpp:176
crocoddyl::Stopwatch::pause
void pause(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
Definition: stop-watch.cpp:146
crocoddyl::Stopwatch::get_max_time
long double get_max_time(const std::string &perf_name)
Returns maximum execution time of a certain performance.
Definition: stop-watch.cpp:285
crocoddyl::Stopwatch::PerformanceData::stops
int stops
How many cycles have been this stopwatch executed?
Definition: stop-watch.hpp:232
crocoddyl::Stopwatch::PerformanceData::paused
bool paused
Tells if this performance has been paused, only for internal use.
Definition: stop-watch.hpp:231
crocoddyl::Stopwatch::get_average_time
long double get_average_time(const std::string &perf_name)
Returns average execution time of a certain performance.
Definition: stop-watch.cpp:267
crocoddyl::Stopwatch::stop
void stop(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
Definition: stop-watch.cpp:117
crocoddyl::Stopwatch
A class representing a stopwatch.
Definition: stop-watch.hpp:155
crocoddyl::Stopwatch::get_last_time
long double get_last_time(const std::string &perf_name)
Return last measurement of a certain performance.
Definition: stop-watch.cpp:294
crocoddyl::Stopwatch::active
bool active
Flag to hold the clock's status.
Definition: stop-watch.hpp:235
crocoddyl::Stopwatch::PerformanceData::clock_start
long double clock_start
Start time.
Definition: stop-watch.hpp:226
crocoddyl::Stopwatch::Stopwatch
Stopwatch(StopwatchMode _mode=NONE)
Constructor.
Definition: stop-watch.cpp:47
crocoddyl::Stopwatch::turn_off
void turn_off()
Turn off clock, all the Stopwatch::* methods return without doing anything after this method is calle...
Definition: stop-watch.cpp:220
crocoddyl::Stopwatch::PerformanceData::last_time
long double last_time
Last time.
Definition: stop-watch.hpp:230
crocoddyl::Stopwatch::set_mode
void set_mode(StopwatchMode mode)
Initialize stopwatch to use a certain time taking mode.
Definition: stop-watch.cpp:53
crocoddyl::Stopwatch::PerformanceData::max_time
long double max_time
Maximum time.
Definition: stop-watch.hpp:229