What are timer ticks, the unit used by Stopwatch.ElapsedTicks
Asked Answered
G

3

11

I used to think that

  • Stopwatch.ElapsedTicks was equal to
  • Stopwatch.Elapsed.Ticks.

But it isn't. While the latter is a number of 100 nanoseconds periods, the former use a mysterious unit called timer ticks. I wonder what those are, and why are they different from the usual unit of measurement?

Gorlovka answered 26/4, 2011 at 17:3 Comment(0)
W
10

From the docs:

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

Yes, it's a pain that "ticks" is overloaded to mean two different things :(

I think it's basically because Stopwatch is giving a pretty "raw" view of a performance counter, which can have different implementations for what it means by a "tick".

Whig answered 26/4, 2011 at 17:5 Comment(0)
M
5

The Stopwatch.ElapsedTicks is measuring a "tick" in terms of the stopwatch, which is a length of time of 1 second/Stopwatch.Frequency.

Internally, this is based on the Windows High Performance Counter Support (if supported by your system, which is almost always true). The native call is QueryPerformanceFrequency, which will vary in length depending on your hardware's support.

Marcum answered 26/4, 2011 at 17:6 Comment(0)
C
0

Maybe it helps to see console output ...

LinkedList 500 insert/remove operations ElapsedTicks : 45871
LinkedList 500 insert/remove operations Elapsed.ticks : 141266
LinkedList 500 insert/remove operations milisec: 14.1266
List 500 insert/remove operations ElapsedTicks: 1235121
List 500 insert/remove operations Elapsed.ticks : 3803744
List 500 insert/remove operations milisec: 380.3744

(From which it is confirmed that Elapsed.Ticks are measured in 100 nanoseconds)

Cote answered 4/5, 2011 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.