I’m trying to measure elapsed time using Stopwatch but consistently get times which are lower ~3.1 times than actual elapsed time. I can’t put my finger on why this happens, code is dead simple. I’ve tried the code on 3 machines and getting same result (although with nearly same hardware and software).
i5-2500k, i5-2500, Windows 7 64 bit, Windows 8 RTM 64 bit
Stopwatch sw = new Stopwatch();
sw.Restart();
DateTime start = DateTime.Now;
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
}
DateTime end = DateTime.Now;
sw.Stop();
Console.WriteLine("Time passed: " + StringFormatter.ToShortString(sw));
Console.WriteLine("Time passed: " + StringFormatter.ToShortString(end - start));
result is consistently ~3.1 times off.
Time passed: 00:00:01.6347
Time passed: 00:00:05.0600
for 10 sec delays:
Time passed: 00:00:03.2358
Time passed: 00:00:10.0100
Ok, this was stupid.
Stopwatch.ElapsedTickswas being used as if it was timespan. Resolution needed to be adjusted for printing logic.