122

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.

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Dariusz
  • 15,573
  • 9
  • 52
  • 68
  • 1
    The `Stop` method prevents you from careless mistakes, for example if you're evaluating the `Elapsed` property multiple times. – Tim Schmelter Jun 10 '14 at 12:00
  • 3
    Take a look at the source: http://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/Stopwatch.cs – Kris Vandermotten Jun 10 '14 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 '14 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 '22 at 19:34

4 Answers4

119

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Uriil
  • 11,948
  • 11
  • 47
  • 68
  • 4
    But it is good practice to use it anyway to prevent careless mistakes. – Tim Schmelter Jun 10 '14 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". – Ronan Thibaudau Jun 10 '14 at 12:23
  • 40
    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: https://github.com/dotnet/corefx/blob/master/src/System.Runtime.Extensions/src/System/Diagnostics/Stopwatch.cs/#L72 – Sámal Rasmussen Mar 02 '16 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 '22 at 20:35
  • @JobaDiniz Stopwatch isn't an IDisposable. – nawfal Nov 16 '22 at 19:32
  • @RonanThibaudau I think Tim's comment is more general. Sure in OP's case it is irrelevant, but in general it is good practice to wrap begin and end statements. Imagine Stopwatch reference being shared. Also see vvv's answer. – nawfal Nov 16 '22 at 19:36
  • Exactly @nawfal, and that’s my point – JobaDiniz Nov 17 '22 at 00:42
71

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.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
4

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

vvv
  • 378
  • 4
  • 12
  • But I don't need in my example :) – Dariusz Jun 10 '14 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 '14 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 '14 at 12:17
-2

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

funbrain9
  • 503
  • 6
  • 15