14

How long can the Stopwatch in .NET run? Does it wrap to negative or restart at 0 if it gets to that limit?

4 Answers4

13

Stopwatch.Elapsed returns a TimeSpan. From MSDN for the MaxValue of TimeSpan:

The value of this field is equivalent to Int64.MaxValue ticks. The string representation of this value is positive 10675199.02:48:05.4775807, or slightly more than 10,675,199 days.

  • 3
    Surely it matters - what happens if you ask for `Elapsed` after 10,675,200 days? – Blorgbeard Oct 17 '17 at 16:08
  • 2
    @PanagiotisKanavos `Stopwatch`'s internal value is a `long`, just like `TimeSpan`'s, so the limit is the same. – Benjamin Hodgson Oct 17 '17 at 16:08
  • 1
    @PanagiotisKanavos They are both measuring ticks, so it's the same number with the same precision, min/max values, and representation. – Servy Oct 17 '17 at 16:09
  • 4
    This answer is correct if your stopwatch frequency equals 10,000,000. This frequency can however change if the underlying timing mechanism changes. Read the remarks on https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=vs.110).aspx for all info. If the frequency would change then you probably still have millions of days before the stopwatch hits it maximum. – Martin Jul 19 '18 at 08:32
  • @Servy They're actually not measuring ticks in the same unit. From https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch.elapsedticks?view=net-6.0: "Each tick in the DateTime.Ticks value represents one 100-nanosecond interval. Each tick in the ElapsedTicks value represents the time interval equal to 1 second divided by the Frequency." – mycroes Mar 28 '22 at 07:48
10

For managed code, the System.Diagnostics.Stopwatch class uses QPC as its precise time basis.

When QPC is available, which it always is on Windows XP or later.

How often does QPC roll over?

Not less than 100 years from the most recent system boot, and potentially longer based on the underlying hardware timer used. For most applications, rollover isn't a concern.

Acquiring high-resolution time stamps

Assuming Microsoft's Windows .NET implementation, this limit is hardware-specific and may be less than the size of a TimeSpan or a long as the other answers indicated. Still plenty large enough though. Good luck getting your program to run for 100 years!

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Worth adding how QPC is related to stopwatch (because it can use simple `DateTime.Now.Ticks` instead of it). – Evk Oct 17 '17 at 16:17
1

Stopwatch has an ElapsedTicks property (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch.elapsedticks), which has the following remark:

This property represents the number of elapsed ticks in the underlying timer mechanism. A tick is the smallest unit of time that the Stopwatch timer can measure. Use the Frequency field to convert the ElapsedTicks value into a number of seconds.

Stopwatch.Frequency is 10000000 on my system. Given the calculation long.MaxValue / Stopwatch.Frequency / 3600 / 24 / 365 results in 29247, that is 29247 (floored) years of 365 days (which obviously they aren't always, but I guess it's close enough).

I also checked the source of current .NET and reflected upon .NET Framework and both use a long field elapsed which it uses to keep track of the elapsed time in the paused state. However, in the running state it uses a startTimeStamp and diffs the current timestamp with the start timestamp and adds previously elapsed time from the elapsed field. However even if the current timestamp returned by the system would have overflowed this should still return the proper result, as long as total elapsed ticks (expressed in Stopwatch.Frequency units, not DateTime.Ticks) fits in a long.

Last but not least, in Stopwatch.Stop() there's a check if the elapsed value is negative and if it is it's reset to 0. There's a comment mentioning this is possible under certain circumstances (bugs in BIOS or HAL, according to the comment) when very small intervals are measured, but the same obviously applies when elapsed overflows and becomes negative.

mycroes
  • 645
  • 8
  • 20
0

StopWatch uses long data type whose max limit is -

9,223,372,036,854,775,807

So this is the max time(ticks) it can run.

Although it would take years to complete this time.

Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38