Welcome to MSDN Blogs Sign in | Join | Help

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. :)
Published Tuesday, July 18, 2006 3:18 PM by ricom
Filed under:

Comments

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

Calling Reset() takes CPU time.  Your colleague's code ran faster without that call  ^_^
Tuesday, July 18, 2006 8:26 PM by Norman Diamond

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

You just befriended a hell lotta people, Rico . . . including me. Wanna catch up tomorrow for coffee?
Tuesday, July 18, 2006 11:12 PM by Kent Boogaart

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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... ;)
Wednesday, July 19, 2006 12:13 AM by Joe Duffy

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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*
Wednesday, July 19, 2006 12:35 AM by ricom

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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 :-)
Wednesday, July 19, 2006 1:07 AM by kcwalina

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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 :)
Wednesday, July 19, 2006 3:13 AM by ricom

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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);
Wednesday, July 19, 2006 10:52 AM by Gord

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

Ya. I made the same mistake. I wish the class provides Pause !!
Wednesday, July 19, 2006 12:33 PM by gc

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

People from the show business have known for ages that doing something embarrassing can increase your fan-base :-)
Thursday, July 20, 2006 12:18 AM by kcwalina

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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.
Thursday, July 20, 2006 12:21 AM by kcwalina

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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.
Thursday, July 20, 2006 11:57 AM by gc

# re: System.Diagnostics.Stopwatch -- always remeber to use Reset

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
Saturday, July 22, 2006 7:55 PM by Alois Kraus
New Comments to this post are disabled
 
Page view tracker