Should I Stop Stopwatch at the end of the method?

Dariusz picture Dariusz · Jun 10, 2014 · Viewed 22.7k times · Source

Let's imagine we have simple measurements using Stopwatch

public void DoWork()
{
    var timer = Stopwatch.StartNew();
    // some hard work
    Logger.Log("Time elapsed: {0}", timer.Elapsed);
    timer.Stop(); // Do I need to call this?
}

According to MSDN:

In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

I'm not sure if I should call this method when I'm no longer interested with timer instance. Should I "clear up" using Stop method?

EDIT

Keep in mind that Logger.Log(..) costs nothing because timer.Elapsed is read before the logger logs.

Answer

Uriil picture Uriil · Jun 10, 2014

No, you don't need to stop it. Stop() just stops tracking elapsed time. It does not free up any resources.