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 (12)

  1. Norman Diamond says:

    Calling Reset() takes CPU time.  Your colleague’s code ran faster without that call  ^_^

  2. You just befriended a hell lotta people, Rico . . . including me. Wanna catch up tomorrow for coffee?

  3. Joe Duffy says:

    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… 😉

  4. ricom says:

    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*

  5. Krzysztof Cwalina says:

    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 🙂

  6. ricom says:

    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 🙂

  7. Gord says:

    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);

  8. gc says:

    Ya. I made the same mistake. I wish the class provides Pause !!

  9. Krzysztof Cwalina says:

    People from the show business have known for ages that doing something embarrassing can increase your fan-base 🙂

  10. Krzysztof Cwalina says:

    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.

  11. gc says:

    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.

  12. Alois Kraus says:

    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