Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multicore and thread aware .Net stopwatch?

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)

like image 692
user316 Avatar asked Feb 14 '12 19:02

user316


1 Answers

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

like image 50
Marko Avatar answered Sep 28 '22 05:09

Marko