Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Stopwatch.ElapsedTicks and Stopwatch.Elapsed.Ticks always the same?

Tags:

.net

stopwatch

What does ElapsedTicks and Elapsed.Ticks in the Stopwatch class mean? When could the meaning be different than intended?

like image 818
Fredou Avatar asked Jun 19 '09 12:06

Fredou


People also ask

What elapsed ticks?

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.

How long is a timer tick?

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

How do you convert ticks to milliseconds?

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.

What is ElapsedMilliseconds?

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 .


1 Answers

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.

like image 128
Fredou Avatar answered Nov 07 '22 07:11

Fredou