BStopWatch

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…

Using a BStopWatch is simple; this…

BStopWatch *watch = new BStopWatch("Timer 0");
/* The code you want to time goes here. */
delete watch;
...

…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:

void MyFunc()
{
   BStopWatch watch("Timer 0");
   ...
}

When the function returns, the BStopWatch prints its message.

Warning
Warning

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.

Creative Commons License
Legal Notice
This work is licensed under a Creative Commons Attribution-Non commercial-No Derivative Works 3.0 License.