What does ElapsedTicks
and Elapsed.Ticks
in the Stopwatch class mean? When could the meaning be different than intended?
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. You can query the properties Elapsed, ElapsedMilliseconds, and ElapsedTicks while the Stopwatch instance is running or stopped.
Each timer tick will be 1/10000 = 0.0001 seconds long, which is 100 microseconds. So if your timer overflows when it reaches 0xFFFF, each overflow will take 65536 ticks, which is 6.5536 seconds (65536 ticks * 0.0001 seconds/tick).
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond (see TicksPerMillisecond) and 10 million ticks in a second.
Elapsed. TotalMilliseconds is a double that can return execution times to the partial millisecond while ElapsedMilliseconds is Int64 . e.g. a stopwatch at 0.0007 milliseconds would return 0, or 1234.56 milliseconds would return 1234 in this property. So for precision always use Elapsed. TotalMilliseconds .
I just found out that ElapsedTicks
in the Stopwatch
class doesn't mean real "ticks" if StopWatch.IsHighResolution
is true
.
Note (if IsHighResolution
is True - from Microsoft Connect link (now dead)):
Stopwatch ticks are different from DateTime.Ticks. 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.
You can do the math above or it seem you can use StopWatch.Elapsed.Ticks
instead of StopWatch.ElapsedTicks
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With