crocoddyl 1.9.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
 
Loading...
Searching...
No Matches
stop-watch.hpp
1
2// 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
43namespace crocoddyl {
44
45// Generic stopwatch exception class
47 public:
48 StopwatchException(std::string error) : error(error) {}
49 std::string error;
50};
51
52enum 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
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
245Stopwatch &getProfiler();
246
247} // namespace crocoddyl
248
249#ifndef WIN32
250#pragma GCC visibility pop
251#endif
252
253#endif
A class representing a stopwatch.
Definition: stop-watch.hpp:150
bool active
Flag to hold the clock's status.
Definition: stop-watch.hpp:239
long double get_total_time(const std::string &perf_name)
Returns total execution time of a certain performance.
Definition: stop-watch.cpp:262
void reset_all()
Resets all the performance records.
Definition: stop-watch.cpp:170
bool profiler_status()
Return if the profiler is enable or disable.
Definition: stop-watch.cpp:55
long double take_time()
Take time, depends on mode.
Definition: stop-watch.cpp:61
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:251
void stop(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
Definition: stop-watch.cpp:121
void turn_on()
Turn on clock, restore clock operativity after a turn_off().
Definition: stop-watch.cpp:219
long double get_last_time(const std::string &perf_name)
Return last measurement of a certain performance.
Definition: stop-watch.cpp:298
std::map< std::string, PerformanceData > * records_of
Dynamic collection of performance data.
Definition: stop-watch.hpp:241
void start(const std::string &perf_name)
Start the stopwatch related to a certain piece of code.
Definition: stop-watch.cpp:103
void reset(const std::string &perf_name)
Reset a certain performance record.
Definition: stop-watch.cpp:202
void disable_profiler()
Disable the profiler.
Definition: stop-watch.cpp:53
bool profiler_active
Indicates if the profiler is enabled.
Definition: stop-watch.hpp:242
void report_all(int precision=2, std::ostream &output=std::cout)
Dump the data of all the performance records.
Definition: stop-watch.cpp:180
long double get_max_time(const std::string &perf_name)
Returns maximum execution time of a certain performance.
Definition: stop-watch.cpp:289
long double get_average_time(const std::string &perf_name)
Returns average execution time of a certain performance.
Definition: stop-watch.cpp:271
void set_mode(StopwatchMode mode)
Initialize stopwatch to use a certain time taking mode.
Definition: stop-watch.cpp:57
~Stopwatch()
Destructor.
Definition: stop-watch.cpp:49
long double get_min_time(const std::string &perf_name)
Returns minimum execution time of a certain performance.
Definition: stop-watch.cpp:280
void turn_off()
Turn off clock, all the Stopwatch::* methods return without doing anything after this method is calle...
Definition: stop-watch.cpp:224
bool performance_exists(std::string perf_name)
Tells if a performance with a certain ID exists.
Definition: stop-watch.cpp:59
void pause(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
Definition: stop-watch.cpp:150
void enable_profiler()
Enable the profiler.
Definition: stop-watch.cpp:51
StopwatchMode mode
Time taking mode.
Definition: stop-watch.hpp:240
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:229
Struct to hold the performance data.
Definition: stop-watch.hpp:226
long double min_time
Minimum time.
Definition: stop-watch.hpp:232
bool paused
Tells if this performance has been paused, only for internal use.
Definition: stop-watch.hpp:235
long double max_time
Maximum time.
Definition: stop-watch.hpp:233
long double clock_start
Start time.
Definition: stop-watch.hpp:230
long double total_time
Cumulative total time.
Definition: stop-watch.hpp:231
int stops
How many cycles have been this stopwatch executed?
Definition: stop-watch.hpp:236