-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch.h
248 lines (177 loc) · 7.31 KB
/
Stopwatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Copyright (c) 2010-2013 Tommaso Urli
Tommaso Urli [email protected] University of Udine
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include "Stdafx.h"
#ifndef WIN32
/* The classes below are exported */
#pragma GCC visibility push(default)
#endif
// Generic stopwatch exception class
struct StopwatchException {
public:
StopwatchException(std::string error) : error(error) { }
std::string error;
};
enum StopwatchMode {
NONE = 0, // Clock is not initialized
CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
REAL_TIME = 2, // Clock calculates time by asking the operating system how much real time passed
};
/**
@brief A class representing a stopwatch.
@code
Stopwatch swatch();
@endcode
The Stopwatch class can be used to measure execution time of code, algorithms, etc., the Stopwatch can
be initialized in two time-taking modes, CPU time and real time:
@code
swatch.set_mode(REAL_TIME);
@endcode
CPU time is the time spent by the processor on a certain piece of code, while real time is the real
amount of time taken by a certain piece of code to execute (i.e. in general if you are doing hard work
such as image or video editing on a different process the measured time will probably increase).
How does it work? Basically, one wraps the code to be measured with the following method calls:
@code
swatch.start("My astounding algorithm");
// Hic est code
swatch.stop("My astounding algorithm");
@endcode
A string representing the code ID is provided so that nested portions of code can be profiled separately:
@code
swatch.start("My astounding algorithm");
swatch.start("My astounding algorithm - Super smart init");
// Initialization
swatch.stop("My astounding algorithm - Super smart init");
swatch.start("My astounding algorithm - Main loop");
// Loop
swatch.stop("My astounding algorithm - Main loop");
swatch.stop("My astounding algorithm");
@endcode
Note: ID strings can be whatever you like, in the previous example I have used "My astounding algorithm - *"
only to enforce the fact that the measured code portions are part of My astounding algorithm, but there's no
connection between the three measurements.
If the code for a certain task is scattered through different files or portions of the same file one can use
the start-pause-stop method:
@code
swatch.start("Setup");
// First part of setup
swatch.pause("Setup");
swatch.start("Main logic");
// Main logic
swatch.stop("Main logic");
swatch.start("Setup");
// Cleanup (part of the setup)
swatch.stop("Setup");
@endcode
Finally, to report the results of the measurements just run:
@code
swatch.report("Code ID");
@endcode
Thou can also provide an additional std::ostream& parameter to report() to redirect the logging on a different
output. Also, you can use the get_total/min/max/average_time() methods to get the individual numeric data, without
all the details of the logging. You can also extend Stopwatch to implement your own logging syntax.
To report all the measurements:
@code
swatch.report_all();
@endcode
Same as above, you can redirect the output by providing a std::ostream& parameter.
*/
class Stopwatch {
public:
/** Constructor */
Stopwatch();
/** Destructor */
~Stopwatch();
/** Tells if a performance with a certain ID exists */
bool performance_exists(std::string perf_name);
/** Initialize stopwatch to use a certain time taking mode */
void set_mode(StopwatchMode mode);
/** Start the stopwatch related to a certain piece of code */
void start(std::string perf_name);
/** Stops the stopwatch related to a certain piece of code */
void stop(std::string perf_name);
/** Stops the stopwatch related to a certain piece of code */
void pause(std::string perf_name);
/** Reset a certain performance record */
void reset(std::string perf_name);
/** Resets all the performance records */
void reset_all();
/** Dump the data of a certain performance record */
void report(std::string perf_name, std::ostream& output = std::cout);
/** Dump the data of all the performance records */
void report_all(std::ostream& output = std::cout);
/** Returns total execution time of a certain performance */
long double get_total_time(std::string perf_name);
/** Returns average execution time of a certain performance */
long double get_average_time(std::string perf_name);
/** Returns minimum execution time of a certain performance */
long double get_min_time(std::string perf_name);
/** Returns maximum execution time of a certain performance */
long double get_max_time(std::string perf_name);
/** Return last measurement of a certain performance */
long double get_last_time(std::string perf_name);
/** Return the time since the start of the last measurement of a given performance. */
long double get_time_so_far(std::string perf_name);
/** Turn off clock, all the Stopwatch::* methods return without doing anything after this method is called. */
void turn_off();
/** Turn on clock, restore clock operativity after a turn_off(). */
void turn_on();
/** Take time, depends on mode */
long double take_time();
protected:
/** Struct to hold the performance data */
struct PerformanceData {
PerformanceData() :
clock_start(0),
total_time(0),
min_time(0),
max_time(0),
last_time(0),
paused(false),
stops(0) {
}
/** Start time */
long double clock_start;
/** Cumulative total time */
long double total_time;
/** Minimum time */
long double min_time;
/** Maximum time */
long double max_time;
/** Last time */
long double last_time;
/** Tells if this performance has been paused, only for internal use */
bool paused;
/** How many cycles have been this stopwatch executed? */
int stops;
};
/** Time taking mode */
StopwatchMode mode;
/** Pointer to the dynamic structure which holds the collection of performance data */
std::map<std::string, PerformanceData >* records_of;
/** Flag to hold the clock's status */
bool active;
};
#ifndef WIN32
#pragma GCC visibility pop
#endif
#endif