14

As we all know the stopwatch might have issues in a multi-threaded async application that will be running on all cores.

On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread.ProcessorAffinity method.

Are there any way for me to get some reliable tick/timestamp like values that has high resolution/accuracy as well is consistent across cpu cores?

If not possible to make the stopwatch(QueryPerformanceCounter) multi-core safe, what is the next best way to get a good timestamp across cores? DateTime.UtcNow.Ticks or Environment.TickCount? Any other counters?

I do need better resolution than what DateTime.UtcNow.Ticks can provide. (10-15ms)

Sam
  • 7,252
  • 16
  • 46
  • 65
user316
  • 309
  • 1
  • 8

2 Answers2

7

I have searched for an answer to this specific problem myself and the best that I have seen is DateTime.UtcNow. It would seem that there is no Windows exposed high resolution and reliable counter (I have googled for one for ages and still haven't come across any).

Marko
  • 2,266
  • 4
  • 27
  • 48
5

Actually, according to the official documentation, post-Windows XP, it should be just fine to use StopWatch / QueryPerformanceCounter on multiprocessor systems. If the system does not support invariant TSC, QPC will automatically use a different timer strategy.

Do note the comment section, though, for virtualized systems. It seems that you have to be careful in that specific case.

Søren Boisen
  • 1,669
  • 22
  • 41