I was trying to "benchmark" two nearly identical loops. When I inspect them one by one the subtraction is without any hiccups, and the TimeSpan gives back the correct numbers as the TotalMilliseconds. When I benchmark the two loops together however the first subtraction gives back a number between 3 - 4 (which should be right), and the second one always gives back 0. What am I doing wrong? Thanks!
class Program
{
static void Main(string[] args)
{
Console.WriteLine(":: Simple for ::");
var a = 0;
var start = DateTime.Now;
for (int i = 0; i < 10; i++)
{
a += i;
}
Console.WriteLine("Elapsed time: {0} ms", (DateTime.Now - start).TotalMilliseconds); // 3 - 4 ms
Console.WriteLine(":: Fancy for ::");
a = 0;
start = DateTime.Now;
foreach (var i in Enumerable.Range(0, 9))
{
a += i;
}
Console.WriteLine("Elapsed time: {0} ms", (DateTime.Now - start).TotalMilliseconds); // 0 ms
}
}