There are many times when you are coding up a solution that you want to get a quick idea for how long a given operation is taking. A common first attempt at this is the following:
1 2 3 4 5 6 7
This test should be self explanatory. You set a start time, invoke a method, output the difference between the current time and the start time. So this is all well and good (I’m not even bringing up the issue of timer resolution), but anytime you want to quickly time an operation you will be duplicating yourself. Remember the DRY principle, it will guide you toward much more maintainable codebases.
Remembering the DRY principle, you start to think of ways to encapsulate the concept of a StopWatch in your codebase. If you are in .Net 2.0, the framework has already taken care of this for you with the addition of the Stopwatch class in the System.Diagnostics namespace. If you are still in .Net 1.1, and want an equivalent to the 2.0 Stopwatch, then head over to Jeff Atwood’s blog and take a look at his Stopwatch implementation. Using either class, you retrofit you code to take advantage of its functionality:
1 2 3 4 5 6 7 8 9
As you can see this takes us a step closer to being able to quickly time methods (for quick curiosity). This still seems a bit verbose for my liking. Anytime I want to time a method using this technique I have to:
- Instantiate a stopwatch
- Start the stopwatch
- Invoke the method to be timed
- Stop the stopwatch
- Output the duration
The only variant in those steps is the method that is going to be invoked (operation to perform). Anyone who knows me, will tell you that I stress the value of eliminating duplication. Let’s eliminate the duplication by introducing a new layer of abstraction:
1 2 3 4 5 6 7 8 9 10 11 12 13
Notice the use of the IDisposable interface. With this class in place, my previous method now changes to this:
1 2 3 4 5 6 7 8
Ahh, duplication be gone. Ok, so what if we now want the ability to time a method executing a certain number of times. We could do this:
1 2 3 4 5 6 7 8 9 10 11
Again, this is something that is often done a lot of times, so why not take advantage of the class we have created and use accordingly (requires some refactoring):
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
As you can see, the MyStopwatch class has undergone a serious facelift. To construct an instance of this, you now have to specify a delegate that points to the method you want to invoke!! You can also optionally specify a number of times you want the method to run. Here is a sample demonstrating how to use the upgraded class:
1 2 3 4 5
Now we are getting somewhere!! Although, I am not sure if I like the fact that the client of the stopwatch has to either use it in a using block, or explicitly call dispose (as in the last example), in order to receive timing details. How about exposing a couple of static methods on MyStopwatch:
1 2 3 4 5 6 7 8
Which leads to a much more convienient usage syntax of:
1 2 3 4 5 6
The nice thing about the convenience methods, is that there is no question about whether or not the client will get its output, as the static methods are ensuring that dispose is getting called on the object. And from my perspective, it eliminates the need for a completely static class (which I am not a big fan of).
If you are a fan of static classes (which depending on what you are doing, could become the bane of your developer existence), you could also use the following Stopwatch implementation (which ends up being a lot more compact):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
And of course, to top it off, if you want to allow for a more flexible mechanism of outputting the results you could do this:
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
Notice the convenience methods that will pass in the Console.Out as the TextWriter to use. This leaves you the ability to pass in your own TextWriter; if you want to,for example, write to a file.
I hope the “timing” of this blog entry was good for you, and that it helped spark you brain with new ideas for implementing your own (ad-hoc) timers in your applications.