On my computer the Stopwatch is returning values way too low. For example, 200 ms when I specified Thread.Sleep(1000)
. The program is supposed to wait 1 second. I also tested with ManualResetEvent.WaitOne(1000)
and got the same results. Both framework 2.0 and 3.0 gives this strange behavior. I am running Windows XP SP3 with .NET Framework 3.5 SP1.
Here is the result of my tests (code below):
1000 ms for DateTime.Now.Ticks
0201 ms for Stopwatch.ElapsedTicks
0142 ms for Stopwatch.ElapsedMilliseconds
0139 ms for Stopwatch.ElapsedTicks after Reset
0264 ms for Stopwatch.ElapsedTicks setting ThreadAffinity
0151 ms for Stopwatch.ElapsedTicks setting ProcessorAffinity (and more)
0371 ms for Stopwatch.ElapsedTicks with Syncronized object
Done!
// Program.cs file
class Program
{
static void Main(string[] args)
{
StopWatchTest.Go();
Console.WriteLine("Done!");
Console.ReadLine();
}
}
// StopWatchTest.cs class
internal static class StopWatchTest
{
public const int SleepTime = 1000;
public static void Go()
{
#region Test #0 with DateTime.Now.Ticks
long startTick = DateTime.Now.Ticks;
Thread.Sleep(SleepTime);
long stopTick = DateTime.Now.Ticks;
long elapsedDt = (stopTick - startTick) * 100;
Display((int)(elapsedDt / 1000 / 1000), "DateTime.Now.Ticks");
#endregion Test #0 with DateTime.Now.Ticks
Stopwatch watch = Stopwatch.StartNew();
long frequency = Stopwatch.Frequency;
double nanosecPerTick = (1000.0 * 1000.0 * 1000.0) / frequency;
#region Test #1 with Stopwatch.ElapsedTicks
startTick = watch.ElapsedTicks;
Thread.Sleep(SleepTime);
stopTick = watch.ElapsedTicks;
double elapsedSw = (stopTick - startTick) * nanosecPerTick;
Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks");
#endregion Test #1 with Stopwatch.ElapsedTicks
#region Test #2 with Stopwatch.ElapsedMilliseconds
startTick = watch.ElapsedMilliseconds;
Thread.Sleep(SleepTime);
stopTick = watch.ElapsedMilliseconds;
Display((int)(stopTick - startTick), "Stopwatch.ElapsedMilliseconds");
#endregion Test #2 with Stopwatch.ElapsedMilliseconds
#region Test #3 with Stopwatch.ElapsedTicks after Reset
watch.Stop();
watch.Reset();
watch.Start();
startTick = watch.ElapsedTicks;
Thread.Sleep(SleepTime);
stopTick = watch.ElapsedTicks;
elapsedSw = (stopTick - startTick) * nanosecPerTick;
Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks after Reset");
#endregion Test #3 with Stopwatch.ElapsedTicks after Reset
#region Test #4 with Stopwatch.ElapsedTicks and ThreadAffinity
Thread.BeginThreadAffinity();
startTick = watch.ElapsedTicks;
Thread.Sleep(SleepTime);
stopTick = watch.ElapsedTicks;
elapsedSw = (stopTick - startTick) * nanosecPerTick;
Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks setting ThreadAffinity");
Thread.EndThreadAffinity();
#endregion Test #4 with Stopwatch.ElapsedTicks and ThreadAffinity
#region Test #5 with Stopwatch.ElapsedTicks and ProcessorAffinity (and more)
const int affinity = 0x0001;
Process proc = Process.GetCurrentProcess();
proc.ProcessorAffinity = new IntPtr(affinity);
proc.PriorityClass = ProcessPriorityClass.High;
ProcessThreadCollection ptc = proc.Threads;
foreach (ProcessThread pt in ptc)
{
pt.IdealProcessor = 0;
pt.ProcessorAffinity = new IntPtr(affinity);
}
Thread.CurrentThread.Priority = ThreadPriority.Highest;
startTick = watch.ElapsedTicks;
Thread.Sleep(SleepTime);
stopTick = watch.ElapsedTicks;
elapsedSw = (stopTick - startTick) * nanosecPerTick;
Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks setting ProcessorAffinity (and more)");
#endregion Test #5 with ProcessorAffinity and more
#region Test #6 with Syncronized object
elapsedSw = new SyncTimer().Go();
Display((int)(elapsedSw / 1000 / 1000), "Stopwatch.ElapsedTicks with Syncronized object");
#endregion Test #6 with Syncronized object
}
private static void Display(int milliseconds, string testName)
{
Console.WriteLine("{0:0000} ms for {1}", milliseconds, testName);
}
}
[Synchronization]
internal class SyncTimer : ContextBoundObject
{
[MethodImpl(MethodImplOptions.Synchronized)]
public double Go()
{
Stopwatch.StartNew();
long frequency = Stopwatch.Frequency;
double nanosecPerTick = (1000.0 * 1000.0 * 1000.0) / frequency;
long startTick = Stopwatch.GetTimestamp();
Thread.Sleep(StopWatchTest.SleepTime);
long stopTick = Stopwatch.GetTimestamp();
return (stopTick - startTick) * nanosecPerTick;
}
}
Have a look at the following links:
System.Diagnostics.Stopwatch returns negative numbers in Elapsed... properties
It seems there is an issue with Stopwatch accuracy which can lead to it returning negative Elapsed times, and also wildly varying times. If you look at the Connect issue, most of the people there are talking about seeing the issue on a virtual machine, which is where we are seeing the negative Elapsed values issue.
Looking at the QueryPerformanceCounter doc, it seems to imply this issue can happen on multiprocessor systems due to BIOS or hardware abstraction layer bugs, but gives no further information and is not specific about virtualised machines.
As for the solution to this problem... I haven't found one in a lot of googling. You can ignore values less than zero, which is not ideal but workable in some cases. But that doesn't solve your problem - how do you know what values are invalid?
Hope this helps somewhat.