117

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.

4
  • 1
    The Stop method prevents you from careless mistakes, for example if you're evaluating the Elapsed property multiple times. Jun 10, 2014 at 12:00
  • 3
    Take a look at the source: referencesource.microsoft.com/#System/services/monitoring/… Jun 10, 2014 at 12:02
  • Since the timer is going out of scope, why do you believe that calling Stop would do any difference? You can't use the value anyway
    – default
    Jun 10, 2014 at 12:05
  • @default I think OP is concerned if keeping the stopwatch ON is resource consuming. Its not clear if a timer is running in a background thread.
    – nawfal
    Nov 16, 2022 at 19:34

4 Answers 4

111

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

7
  • 4
    But it is good practice to use it anyway to prevent careless mistakes. Jun 10, 2014 at 12:01
  • 15
    It doesn't prevent anything, he's creating the stopwatch in the method and not returning it, so he won't be able to get a hold of it later to "make mistakes with". Jun 10, 2014 at 12:23
  • 37
    Also a Stopwatch isn't doing any work or eating cpu clock cycles between the calls to Start() and Stop(). Start() just sets a timestamp to now and Stop() calculates and saves the time elapsed since that. See source in coreclr: github.com/dotnet/corefx/blob/master/src/… Mar 2, 2016 at 11:33
  • I think, from design perspective, would be better to write using var stopwatch = Stopwatch.StartNew(); instead of call .Start() and then .Stop and handle exceptions in the middle :)
    – JobaDiniz
    Oct 18, 2022 at 20:35
  • @JobaDiniz Stopwatch isn't an IDisposable.
    – nawfal
    Nov 16, 2022 at 19:32
66

No, there is no need to stop or clean it up.

Stopwatch does not use any unmanaged resources (if you thought of IDisposable). It actually does not use any resources at all (except the memory used by the object itself, of course)! It also does not consume any CPU while measuring the elapsed time!

In windows implementations of .NET (full .NET Framework, Mono, .NET Core), it just calls the QueryPerformanceCounter() Windows API when needed (on Start() and Stop() and when reading Elapsed) to retrieve a high resolution time stamp.

In Linux implementations of Mono and .NET Core, it uses clock_gettime function to retrieve a monotonic increasing time value.

To anyone with a real curiosity about the implementation details: read this post.

4

I think Stop is useful if you want to reuse the Elapsed value.

3
  • But I don't need in my example :)
    – Dariusz
    Jun 10, 2014 at 12:05
  • 1
    Then it's great :), I should have used "only" The class doesn't implement Dispose interface, so no cleanup should be trigger.
    – vvv
    Jun 10, 2014 at 12:08
  • 3
    @vvv If you want you can add that to your answer - there's a button between the answer and the comments that says "edit".
    – default
    Jun 10, 2014 at 12:17
-2

If your code doesn't need to calculate, then no need to use Stop().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.