-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
80 lines (52 loc) · 2.64 KB
/
README
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
How to use this code
====================
Not surprisingly, Stopwatch is a class representing a stopwatch.
Stopwatch swatch();
A Stopwatch object 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:
watch.set_mode(REAL_TIME);
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:
swatch.start("My astounding algorithm");
// Hic est code
swatch.stop("My astounding algorithm");
A string representing the code ID is provided so that nested portions of code can be profiled separately:
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");
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:
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");
Finally, to report the results of the measurements just run:
swatch.report("Code ID");
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:
swatch.report_all();
Same as above, you can redirect the output by providing a std::ostream& parameter.
License
=======
See LICENSE file.
Drop a line
===========
If you use this code for your software, please let me know with a mail
message at [email protected], or not.