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 #include <sot/core/fwd.hh>
35 
36 #ifndef WIN32
37 /* The classes below are exported */
38 #pragma GCC visibility push(default)
39 #endif
40 
41 // Generic stopwatch exception class
43  public:
44  StopwatchException(std::string error) : error(error) {}
45  std::string error;
46 };
47 
49  NONE = 0, // Clock is not initialized
50  CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
51  REAL_TIME = 2 // Clock calculates time by asking the operating system how
52  // much real time passed
53 };
54 
55 #define STOP_WATCH_MAX_NAME_LENGTH 80
56 
148 class Stopwatch {
149  public:
151  Stopwatch(StopwatchMode _mode = NONE);
152 
154  ~Stopwatch();
155 
157  bool performance_exists(std::string perf_name);
158 
161 
163  void start(std::string perf_name);
164 
166  void stop(std::string perf_name);
167 
169  void pause(std::string perf_name);
170 
172  void reset(std::string perf_name);
173 
175  void reset_all();
176 
178  void report(std::string perf_name, int precision = 2,
179  std::ostream &output = std::cout);
180 
182  void report_all(int precision = 2, std::ostream &output = std::cout);
183 
185  long double get_total_time(std::string perf_name);
186 
188  long double get_average_time(std::string perf_name);
189 
191  long double get_min_time(std::string perf_name);
192 
194  long double get_max_time(std::string perf_name);
195 
197  long double get_last_time(std::string perf_name);
198 
201  long double get_time_so_far(std::string perf_name);
202 
205  void turn_off();
206 
208  void turn_on();
209 
211  long double take_time();
212 
213  protected:
217  : clock_start(0),
218  total_time(0),
219  min_time(0),
220  max_time(0),
221  last_time(0),
222  paused(false),
223  stops(0) {}
224 
226  long double clock_start;
227 
229  long double total_time;
230 
232  long double min_time;
233 
235  long double max_time;
236 
238  long double last_time;
239 
241  bool paused;
242 
244  int stops;
245  };
246 
248  bool active;
249 
252 
255  std::map<std::string, PerformanceData> *records_of;
256 };
257 
259 
260 #ifndef WIN32
261 #pragma GCC visibility pop
262 #endif
263 
264 #endif
CPU_TIME
@ CPU_TIME
Definition: stop-watch.hh:50
Stopwatch::stop
void stop(std::string perf_name)
Definition: stop-watch.cpp:123
Stopwatch::get_average_time
long double get_average_time(std::string perf_name)
Definition: stop-watch.cpp:275
StopwatchException
Definition: stop-watch.hh:42
Stopwatch::get_max_time
long double get_max_time(std::string perf_name)
Definition: stop-watch.cpp:295
Stopwatch::get_last_time
long double get_last_time(std::string perf_name)
Definition: stop-watch.cpp:305
NONE
@ NONE
Definition: stop-watch.hh:49
Stopwatch::PerformanceData::max_time
long double max_time
Definition: stop-watch.hh:235
Stopwatch::~Stopwatch
~Stopwatch()
Definition: stop-watch.cpp:55
Stopwatch::get_min_time
long double get_min_time(std::string perf_name)
Definition: stop-watch.cpp:285
Stopwatch::PerformanceData
Definition: stop-watch.hh:215
Stopwatch::active
bool active
Definition: stop-watch.hh:248
Stopwatch::performance_exists
bool performance_exists(std::string perf_name)
Definition: stop-watch.cpp:59
Stopwatch::PerformanceData::paused
bool paused
Definition: stop-watch.hh:241
StopwatchMode
StopwatchMode
Definition: stop-watch.hh:48
Stopwatch::PerformanceData::PerformanceData
PerformanceData()
Definition: stop-watch.hh:216
Stopwatch
A class representing a stopwatch.
Definition: stop-watch.hh:148
Stopwatch::take_time
long double take_time()
Definition: stop-watch.cpp:63
fwd.hh
Stopwatch::get_time_so_far
long double get_time_so_far(std::string perf_name)
Definition: stop-watch.cpp:252
Stopwatch::Stopwatch
Stopwatch(StopwatchMode _mode=NONE)
Definition: stop-watch.cpp:51
Stopwatch::set_mode
void set_mode(StopwatchMode mode)
Definition: stop-watch.cpp:57
Stopwatch::PerformanceData::stops
int stops
Definition: stop-watch.hh:244
REAL_TIME
@ REAL_TIME
Definition: stop-watch.hh:51
Stopwatch::PerformanceData::last_time
long double last_time
Definition: stop-watch.hh:238
Stopwatch::pause
void pause(std::string perf_name)
Definition: stop-watch.cpp:154
Stopwatch::turn_off
void turn_off()
Definition: stop-watch.cpp:219
Stopwatch::report
void report(std::string perf_name, int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:224
Stopwatch::mode
StopwatchMode mode
Definition: stop-watch.hh:251
Stopwatch::turn_on
void turn_on()
Definition: stop-watch.cpp:214
getProfiler
Stopwatch & getProfiler()
Definition: stop-watch.cpp:46
Stopwatch::records_of
std::map< std::string, PerformanceData > * records_of
Definition: stop-watch.hh:255
Stopwatch::PerformanceData::clock_start
long double clock_start
Definition: stop-watch.hh:226
Stopwatch::get_total_time
long double get_total_time(std::string perf_name)
Definition: stop-watch.cpp:265
StopwatchException::error
std::string error
Definition: stop-watch.hh:45
Stopwatch::reset_all
void reset_all()
Definition: stop-watch.cpp:175
StopwatchException::StopwatchException
StopwatchException(std::string error)
Definition: stop-watch.hh:44
Stopwatch::PerformanceData::min_time
long double min_time
Definition: stop-watch.hh:232
Stopwatch::start
void start(std::string perf_name)
Definition: stop-watch.cpp:105
Stopwatch::reset
void reset(std::string perf_name)
Definition: stop-watch.cpp:196
Stopwatch::report_all
void report_all(int precision=2, std::ostream &output=std::cout)
Definition: stop-watch.cpp:185
Stopwatch::PerformanceData::total_time
long double total_time
Definition: stop-watch.hh:229


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:31