Comment 4 for bug 1072379

Revision history for this message
Geoffroy Querol (geoffroy-querol) wrote :

Hello everyone,

Just a few notes on the current stopwatch implementation of microsoft, it has a few issues on multicore systems :
(noted in the reference page http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx)

in short 1): if the stopwatch instance is used on a thread for which the processor affinity is not set to a single logical core, the stopwatch measurements are unreliable (they will report negative times, unbelieavable times like 100 times the amount).

2) from my personal experience with this class, i tried to work arround this (setting processor afinity is clearly what you dont want to deal with) using the StopWatch.GetTimeStamp() method which is supposed give a high resolution timer if the underlying system supports it. this amount of ticks can be converted into ms like that:

var start = StopWatch.GetTimeStamp();
var end = StopWatch.GetTimeStamp();
long elapsedms = ((end - start) / StopWatch.Frequency;

On the plus side, this static method might be lighter and lower level than the instance usage.

Btw, thanks for your hard work.