Loading...
Searching...
No Matches
stop-watch.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2010-2013 Tommaso Urli
3
4Tommaso Urli tommaso.urli@uniud.it University of Udine
5
6Permission is hereby granted, free of charge, to any person obtaining
7a copy of this software and associated documentation files (the
8"Software"), to deal in the Software without restriction, including
9without limitation the rights to use, copy, modify, merge, publish,
10distribute, sublicense, and/or sell copies of the Software, and to
11permit persons to whom the Software is furnished to do so, subject to
12the following conditions:
13
14The above copyright notice and this permission notice shall be
15included in all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25*/
26
27
28#ifndef __invdyn_stopwatch_H__
29#define __invdyn_stopwatch_H__
30
31#include "tsid/utils/Stdafx.hh"
32
33#ifndef WIN32
34/* The classes below are exported */
35#pragma GCC visibility push(default)
36#endif
37
38//#define START_PROFILER(name)
39//#define STOP_PROFILER(name)
40#define START_PROFILER(name) getProfiler().start(name)
41#define STOP_PROFILER(name) getProfiler().stop(name)
42
43#define STOP_WATCH_MAX_NAME_LENGTH 60
44#define STOP_WATCH_TIME_WIDTH 10
45
46// Generic stopwatch exception class
48{
49public:
50 StopwatchException(std::string error) : error(error) { }
51 std::string error;
52};
53
54
56{
57 NONE = 0, // Clock is not initialized
58 CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
59 REAL_TIME = 2 // Clock calculates time by asking the operating system how
60 // much real time passed
61};
62
63
156public:
157
160
162 ~Stopwatch();
163
165 bool performance_exists(std::string perf_name);
166
169
171 void start(std::string perf_name);
172
174 void stop(std::string perf_name);
175
177 void pause(std::string perf_name);
178
180 void reset(std::string perf_name);
181
183 void reset_all();
184
186 void report(std::string perf_name, int precision=2,
187 std::ostream& output = std::cout);
188
190 void report_all(int precision=2, std::ostream& output = std::cout);
191
193 long double get_total_time(std::string perf_name);
194
196 long double get_average_time(std::string perf_name);
197
199 long double get_min_time(std::string perf_name);
200
202 long double get_max_time(std::string perf_name);
203
205 long double get_last_time(std::string perf_name);
206
209 long double get_time_so_far(std::string perf_name);
210
213 void turn_off();
214
216 void turn_on();
217
219 long double take_time();
220
221protected:
222
225
227 clock_start(0),
228 total_time(0),
229 min_time(0),
230 max_time(0),
231 last_time(0),
232 paused(false),
233 stops(0) {
234 }
235
237 long double clock_start;
238
240 long double total_time;
241
243 long double min_time;
244
246 long double max_time;
247
249 long double last_time;
250
252 bool paused;
253
255 int stops;
256 };
257
259 bool active;
260
263
266 std::map<std::string, PerformanceData >* records_of;
267
268};
269
271
272#ifndef WIN32
273#pragma GCC visibility pop
274#endif
275
276#endif
A class representing a stopwatch.
Definition: stop-watch.hpp:155
std::map< std::string, PerformanceData > * records_of
Definition: stop-watch.hpp:266
long double get_time_so_far(std::string perf_name)
Definition: stop-watch.cpp:277
long double take_time()
Definition: stop-watch.cpp:71
long double get_min_time(std::string perf_name)
Definition: stop-watch.cpp:316
long double get_max_time(std::string perf_name)
Definition: stop-watch.cpp:328
void reset(std::string perf_name)
Definition: stop-watch.cpp:221
long double get_total_time(std::string perf_name)
Definition: stop-watch.cpp:292
void set_mode(StopwatchMode mode)
Definition: stop-watch.cpp:61
bool performance_exists(std::string perf_name)
Definition: stop-watch.cpp:66
~Stopwatch()
Definition: stop-watch.cpp:56
void report_all(int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:203
void turn_on()
Definition: stop-watch.cpp:240
void start(std::string perf_name)
Definition: stop-watch.cpp:116
long double get_average_time(std::string perf_name)
Definition: stop-watch.cpp:304
bool active
Definition: stop-watch.hpp:259
void pause(std::string perf_name)
Definition: stop-watch.cpp:169
void reset_all()
Definition: stop-watch.cpp:192
void stop(std::string perf_name)
Definition: stop-watch.cpp:135
void turn_off()
Definition: stop-watch.cpp:246
long double get_last_time(std::string perf_name)
Definition: stop-watch.cpp:340
StopwatchMode mode
Definition: stop-watch.hpp:262
void report(std::string perf_name, int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:252
StopwatchMode
Definition: stop-watch.hpp:56
@ CPU_TIME
Definition: stop-watch.hpp:58
@ REAL_TIME
Definition: stop-watch.hpp:59
@ NONE
Definition: stop-watch.hpp:57
Stopwatch & getProfiler()
Definition: stop-watch.cpp:44
Definition: stop-watch.hpp:48
StopwatchException(std::string error)
Definition: stop-watch.hpp:50
std::string error
Definition: stop-watch.hpp:51
Definition: stop-watch.hpp:224
long double min_time
Definition: stop-watch.hpp:243
long double total_time
Definition: stop-watch.hpp:240
long double max_time
Definition: stop-watch.hpp:246
int stops
Definition: stop-watch.hpp:255
bool paused
Definition: stop-watch.hpp:252
long double last_time
Definition: stop-watch.hpp:249
long double clock_start
Definition: stop-watch.hpp:237
PerformanceData()
Definition: stop-watch.hpp:226