VON#
Confessions of a code junkie
RSS
Tuesday, January 15, 2008
StopwatchWriter Class
Do you ever get sick of having to write 4 whole lines for timing certain parts of your code. I know 4 lines isn't that bad, but they multiply quickly when you're testing a lot of different code, or different parts of the same method. That is why I came up with a StopwatchWriter class, it implements IDisposable so you can use it in a using statement which reduces the needed code to one line, put at the top of the code your testing (as opposed to before to setup and after to write). The constructor starts a stopwatch and when the Dispose method gets called, it stops the stopwatch and writes the time to the console, but it could easily be changed to write any type of log.
public class StopwatchWriter : IDisposable
{
Stopwatch _stopwatch = new Stopwatch();
string _text;
public StopwatchWriter(string text)
{
_text = text + " - ";
_stopwatch.Start();
}
public void Dispose()
{
_stopwatch.Stop();
Console.WriteLine("stopw: "+_text + _stopwatch.ElapsedMilliseconds);
}
}
Usage looks like:
using (new StopwatchWriter("populateStuff"))
{
this.PopulateStep1();
this.PopulateStep2();
}
-or-
using (new StopwatchWriter("doStuff"))
DoStuff();
Tuesday, January 15, 2008 - 9:06 AM CST -
Permalink
Comments [3]
-
Tags:
Tips and Tricks
Wednesday, January 16, 2008 3:39:25 PM (Central Standard Time, UTC-06:00)
Hi - what about mentioning the "source of inspiration" of your StopwatchWriter? I mean it's too similar to Joe Chengs implementation to be a coincidence, only you have added the use of stopwatch as he mentions in his blog post:
http://jcheng.wordpress.com/2007/10/23/code-sample-quicktimer-2/
wwfDev
Wednesday, January 16, 2008 11:51:00 PM (Central Standard Time, UTC-06:00)
wwfDev - This is a common pattern and could easily have been developed without ever seeing your referenced post...
Now that I've seen both blog entries I hope to remember to use the pattern for the next time I run in to this situation!
kevin
Thursday, January 17, 2008 9:41:00 AM (Central Standard Time, UTC-06:00)
wwfDev - yeah, Kevin is right, this is a pretty common pattern, when I first came across it, it was being used to wrap a "stuffIsChanging" private instance bool, which would prevent infinite recursion on some winform dropdown selection changed events. In fact, I decided to share my class when Robert Prouse wrote his Stopwatch (http://www.alteridem.net/2008/01/14/the-stopwatch-class-in-net/). I left a comment on his post commenting about this pattern and he even said that he was planning on writing a similar post, I just beat him to it.
Thanks for reading
Jon von Gillern
Comments are closed.
All Content © 2009, Jon von Gillern
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Home
Tags
Blend
Design
IADNUG
Projects
Stuff
Tips and Tricks
Wishlist
Archive
October, 2008 (1)
September, 2008 (1)
August, 2008 (1)
July, 2008 (2)
June, 2008 (2)
May, 2008 (2)
March, 2008 (2)
February, 2008 (8)
January, 2008 (4)
December, 2007 (6)
November, 2007 (2)
Blogroll
Eric Sink
Javier Lozano
Jeff Atwood
Joel Spolsky
Nick Parker
Paul Graham
Scott Hanselman
Tim Gifford