BStopWatch
objects are useful if you want to get an idea of where your
cycles are going. But you shouldn't rely on them for painfully accurate
measurements.
The BStopWatch
class is a debugging tool designed to time the execution
of portions of your code. When a
BStopWatch
object is constructed, it
starts an internal timer. When it's deleted, it stops the timer and
prints the elapsed time to standard out in this format:
StopWatch "name
":f
usecs.
Where name
is the name that
you gave to the object when you constructed
it, and f
is the elapsed time in microseconds.
Look at all these other things you can do…
Suspend, resume, and reset the timer
(Suspend()
,
Resume()
,
Reset()
).
Ask for the current elapsed time without stopping the timer
(ElapsedTime()
).
Record "lap points" that are printed out at the end of the run
(Lap()
).
Using a BStopWatch
is simple; this…
BStopWatch
*watch
= newBStopWatch
("Timer 0"); /* The code you want to time goes here. */ deletewatch
; ...
…will produce, on standard out, a message that goes something like this:
StopWatch "Timer 0": 492416 usecs.
This would indicate that the timed code took about half a second to execute—remember, you're looking at microseconds.
If you want to time an entire function, just toss a StopWatch on the stack:
voidMyFunc
() {BStopWatch
watch
("Timer 0"); ... }
When the function returns, the
BStopWatch
prints its message.
BStopWatch
objects are useful if you want to get an idea of where your
cycles are going. But you shouldn't rely on them for painfully accurate
measurements.