Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences in Elapsed Ticks property of Stopwatch

ElapsedTicks & Elapsed.Ticks are properties of Stopwatch, which I think should be same. And in case they are same, why they should give different outputs ?

Code :

Stopwatch spwt = Stopwatch.StartNew();
spwt.Stop();
Console.WriteLine(spwt.ElapsedTicks);
Console.WriteLine(spwt.Elapsed.Ticks);

Output :

6
16

Why is this difference observed ? shouldn't it be same ?

like image 544
Pratik Avatar asked Sep 15 '11 14:09

Pratik


People also ask

What is elapsed time in Stopwatch?

By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. Each call to Start begins counting at the cumulative elapsed time; each call to Stop ends the current interval measurement and freezes the cumulative elapsed time value.

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 accurate is the Stopwatch class?

Stopwatch class does accurately measure time elapsed, but the way that the ElapsedTicks method works has led some people to the conclusion that it is not accurate, when they really just have a logic error in their code.


1 Answers

See https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch.elapsedticks#remarks :

Note

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.

like image 154
C.Evenhuis Avatar answered Oct 16 '22 05:10

C.Evenhuis