Nitriq is a product by
Home
|
Console Edition
|
Features
|
Getting Started
|
Documentation
|
Support
|
Blog
|
About
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.
Click
Download Nitriq Now
for download options on the
NimblePros download page.
RSS
Tags
Atomiq
Blend
CopyPasteKiller
Design
Flamebait
fun
Hacker News
IADNUG
Nitriq
Projects
Stuff
Tips and Tricks
Wishlist
XAML
Archive
November, 2011 (2)
April, 2011 (1)
March, 2011 (2)
November, 2010 (1)
September, 2010 (1)
July, 2010 (2)
June, 2010 (2)
May, 2010 (1)
April, 2010 (1)
March, 2010 (1)
November, 2009 (1)
October, 2009 (5)
September, 2009 (1)
August, 2009 (1)
July, 2009 (1)
June, 2009 (1)
May, 2009 (1)
April, 2009 (2)
March, 2009 (1)
February, 2009 (2)
January, 2009 (1)
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)