31

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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Fredou
  • 19,848
  • 10
  • 58
  • 113

2 Answers2

25

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Fredou
  • 19,848
  • 10
  • 58
  • 113
12
Elapsed.Ticks / TimeSpan.TicksPerSecond == ElapsedTicks / Stopwatch.Frequency

Of course this may not exactly equal due to rounding, as Stopwatch ticks and TimeSpan ticks are measured in different units. Also, in case you executed the above code literally, obviously some ticks would elapse between taking the value of Elapsed.Ticks and that of ElapsedTicks.

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156