How long can the Stopwatch
in .NET run? Does it wrap to negative or restart at 0 if it gets to that limit?
-
1According to my calculations, about 30,000 years. – Blorgbeard Oct 17 '17 at 16:05
-
3the HPET hardware specification demands a 64-bit counter. Math.Pow(2, 64) is a nice big number. As pilots of modern spy planes asserted, you'll run out of ass before you run out of gas. – Hans Passant Oct 17 '17 at 17:08
-
Its batteries need to be replaced once in a while. – usr Feb 10 '22 at 14:00
4 Answers
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.
-
3Surely 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
-
4This 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
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!

- 1,447
- 5
- 18
- 25

- 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
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 theElapsedTicks
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.

- 645
- 8
- 20
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.

- 2,872
- 1
- 23
- 38
-
Not DateTime, 2 bits are used to keep track of the Kind property. – Hans Passant Oct 17 '17 at 17:26