System.Diagnostics.Stopwatch -- always remeber to use Reset

Published 18 July 06 03:18 PM | ricom 
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. :)
Filed under:

Comments

# Norman Diamond said on July 18, 2006 8:26 PM:
Calling Reset() takes CPU time.  Your colleague's code ran faster without that call  ^_^
# Kent Boogaart said on July 18, 2006 11:12 PM:
You just befriended a hell lotta people, Rico . . . including me. Wanna catch up tomorrow for coffee?
# Joe Duffy said on July 19, 2006 12:13 AM:
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... ;)
# ricom said on July 19, 2006 12:35 AM:
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*
# kcwalina said on July 19, 2006 1:07 AM:
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 :-)
# ricom said on July 19, 2006 3:13 AM:
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 :)
# Gord said on July 19, 2006 10:52 AM:
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);
# gc said on July 19, 2006 12:33 PM:
Ya. I made the same mistake. I wish the class provides Pause !!
# kcwalina said on July 20, 2006 12:18 AM:
People from the show business have known for ages that doing something embarrassing can increase your fan-base :-)
# kcwalina said on July 20, 2006 12:21 AM:
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.
# gc said on July 20, 2006 11:57 AM:
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.
# Alois Kraus said on July 22, 2006 7:55 PM:
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
New Comments to this post are disabled

Search

This Blog

Syndication

Page view tracker