System.Diagnostics.Stopwatch -- always remeber to use Reset
Wrong
System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
s.Start();
for (i = 0; i < count; i++) Test1();
s.Stop();
Console.WriteLine("Test1: {0}", s.ElapsedMilliseconds);
s.Start();
for (i = 0; i < count; i++) Test2();
s.Stop();
Console.WriteLine("Test2: {0}", s.ElapsedMilliseconds);
Right
System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
s.Start();
for (i = 0; i < count; i++) Test1();
s.Stop();
Console.WriteLine("Test1: {0}", s.ElapsedMilliseconds);
s.Reset();
s.Start();
for (i = 0; i < count; i++) Test2();
s.Stop();
Console.WriteLine("Test2: {0}", s.ElapsedMilliseconds);
Not that *cough* I would ever make this mistake or anything, but you know... it could happen to like, a friend, or something. Ya. My friend. :)
Comments
- Anonymous
July 18, 2006
Calling Reset() takes CPU time. Your colleague's code ran faster without that call ^_^ - Anonymous
July 18, 2006
The comment has been removed - Anonymous
July 18, 2006
Yeah - and hopefully you catch it before the resulting #s make it into a spreadsheet, then a PPT, and then get presented to a bunch of people... Not that I've made that mistake before either, mind you... ;) - Anonymous
July 18, 2006
Yes but you were only doing that so I wouldn't look bad right?
Err, what I mean is... Hypothetically, if that had happened, then that would be hypothetically why it, uh, hypothetically, uh, happened.
Ya. That.
grin - Anonymous
July 18, 2006
Paraphrasing a famous performance architect, I suggest that you measure, measure, and measure … using three different methods to be able to cross check your results :-) - Anonymous
July 18, 2006
You know the irony is I normally can't get my colleagues to read my blog without prodding. I say something embarassing and the whole department rushes online :)
You're all swines :) - Anonymous
July 19, 2006
Call me lazy, but I always end up using the Startnew so I never forget to call reset :)
Stopwatch s = Stopwatch.StartNew();
for (i = 0; i < count; i++) Test1();
s.Stop();
Console.WriteLine("Test1: {0}", s.ElapsedMilliseconds);
s = Stopwatch.StartNew();
for (i = 0; i < count; i++) Test2();
s.Stop();
Console.WriteLine("Test2: {0}", s.ElapsedMilliseconds); - Anonymous
July 19, 2006
Ya. I made the same mistake. I wish the class provides Pause !! - Anonymous
July 19, 2006
People from the show business have known for ages that doing something embarrassing can increase your fan-base :-) - Anonymous
July 19, 2006
GC, adding Pause might not help as much. Many people would never notice we added Pause. Something like "StartAgain" could, as it shows next to Start in documentation and the statement completion drop-down. - Anonymous
July 20, 2006
Hi Krzysztof,
My main point is to add a different function to do reset and start. Sometimes people like me assume what the function would do by looking at the function name :-) even though MSDN does state clearly:
Starting a Stopwatch that is already running does not change the timer state or reset the elapsed time properties. - Anonymous
July 22, 2006
Hi Rico,
nice tip. I was always wondering how reliable this QueryPerformanceCounter thing is with Hyperthreaded, MulitCore or Multiprocessor machines. Imagine if I read the start counter on one processor and in the meantime I am scheduled to another processor. I will then? read the counter of another processor and get totally wrong times. Are there any guarantees that the counters of all cores/processors are synchronized?
Yours,
Alois Kraus