sot-core  4.11.6
Hierarchical task solver plug-in for dynamic-graph.
stop-watch.hh
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2013 Tommaso Urli
3 
4 Tommaso Urli tommaso.urli@uniud.it University of Udine
5 
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13 
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 */
26 
27 #ifndef __sot_core_stopwatch_H__
28 #define __sot_core_stopwatch_H__
29 
30 #include <ctime>
31 #include <iostream>
32 #include <map>
33 #include <sstream>
34 
35 #ifndef WIN32
36 /* The classes below are exported */
37 #pragma GCC visibility push(default)
38 #endif
39 
40 // Generic stopwatch exception class
42 public:
43  StopwatchException(std::string error) : error(error) {}
44  std::string error;
45 };
46 
48  NONE = 0, // Clock is not initialized
49  CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
50  REAL_TIME = 2 // Clock calculates time by asking the operating system how
51  // much real time passed
52 };
53 
54 #define STOP_WATCH_MAX_NAME_LENGTH 80
55 
147 class Stopwatch {
148 public:
150  Stopwatch(StopwatchMode _mode = NONE);
151 
153  ~Stopwatch();
154 
156  bool performance_exists(std::string perf_name);
157 
160 
162  void start(std::string perf_name);
163 
165  void stop(std::string perf_name);
166 
168  void pause(std::string perf_name);
169 
171  void reset(std::string perf_name);
172 
174  void reset_all();
175 
177  void report(std::string perf_name, int precision = 2,
178  std::ostream &output = std::cout);
179 
181  void report_all(int precision = 2, std::ostream &output = std::cout);
182 
184  long double get_total_time(std::string perf_name);
185 
187  long double get_average_time(std::string perf_name);
188 
190  long double get_min_time(std::string perf_name);
191 
193  long double get_max_time(std::string perf_name);
194 
196  long double get_last_time(std::string perf_name);
197 
200  long double get_time_so_far(std::string perf_name);
201 
204  void turn_off();
205 
207  void turn_on();
208 
210  long double take_time();
211 
212 protected:
215 
217  : clock_start(0), total_time(0), min_time(0), max_time(0), last_time(0),
218  paused(false), stops(0) {}
219 
221  long double clock_start;
222 
224  long double total_time;
225 
227  long double min_time;
228 
230  long double max_time;
231 
233  long double last_time;
234 
236  bool paused;
237 
239  int stops;
240  };
241 
243  bool active;
244 
247 
250  std::map<std::string, PerformanceData> *records_of;
251 };
252 
254 
255 #ifndef WIN32
256 #pragma GCC visibility pop
257 #endif
258 
259 #endif
CPU_TIME
@ CPU_TIME
Definition: stop-watch.hh:49
Stopwatch::stop
void stop(std::string perf_name)
Stopwatch::get_average_time
long double get_average_time(std::string perf_name)
StopwatchException
Definition: stop-watch.hh:41
Stopwatch::get_max_time
long double get_max_time(std::string perf_name)
Stopwatch::get_last_time
long double get_last_time(std::string perf_name)
NONE
@ NONE
Definition: stop-watch.hh:48
Stopwatch::PerformanceData::max_time
long double max_time
Definition: stop-watch.hh:230
Stopwatch::~Stopwatch
~Stopwatch()
Stopwatch::get_min_time
long double get_min_time(std::string perf_name)
Stopwatch::PerformanceData
Definition: stop-watch.hh:214
Stopwatch::active
bool active
Definition: stop-watch.hh:243
Stopwatch::performance_exists
bool performance_exists(std::string perf_name)
Stopwatch::PerformanceData::paused
bool paused
Definition: stop-watch.hh:236
StopwatchMode
StopwatchMode
Definition: stop-watch.hh:47
Stopwatch::PerformanceData::PerformanceData
PerformanceData()
Definition: stop-watch.hh:216
Stopwatch
A class representing a stopwatch.
Definition: stop-watch.hh:147
Stopwatch::take_time
long double take_time()
Stopwatch::get_time_so_far
long double get_time_so_far(std::string perf_name)
Stopwatch::Stopwatch
Stopwatch(StopwatchMode _mode=NONE)
Stopwatch::set_mode
void set_mode(StopwatchMode mode)
Stopwatch::PerformanceData::stops
int stops
Definition: stop-watch.hh:239
REAL_TIME
@ REAL_TIME
Definition: stop-watch.hh:50
Stopwatch::PerformanceData::last_time
long double last_time
Definition: stop-watch.hh:233
Stopwatch::pause
void pause(std::string perf_name)
Stopwatch::turn_off
void turn_off()
Stopwatch::report
void report(std::string perf_name, int precision=2, std::ostream &output=std::cout)
Stopwatch::mode
StopwatchMode mode
Definition: stop-watch.hh:246
Stopwatch::turn_on
void turn_on()
getProfiler
Stopwatch & getProfiler()
Stopwatch::records_of
std::map< std::string, PerformanceData > * records_of
Definition: stop-watch.hh:250
Stopwatch::PerformanceData::clock_start
long double clock_start
Definition: stop-watch.hh:221
Stopwatch::get_total_time
long double get_total_time(std::string perf_name)
StopwatchException::error
std::string error
Definition: stop-watch.hh:44
Stopwatch::reset_all
void reset_all()
StopwatchException::StopwatchException
StopwatchException(std::string error)
Definition: stop-watch.hh:43
Stopwatch::PerformanceData::min_time
long double min_time
Definition: stop-watch.hh:227
Stopwatch::start
void start(std::string perf_name)
Stopwatch::reset
void reset(std::string perf_name)
Stopwatch::report_all
void report_all(int precision=2, std::ostream &output=std::cout)
Stopwatch::PerformanceData::total_time
long double total_time
Definition: stop-watch.hh:224