用Stopwatch类获得程序运行时间
我们可以用Stopwatch类获得程序的运行时间,在优化代码时,可以用此方法来查看优化前后程序所耗费的时间
//Stopwatch类別在System.Diagnostics命名空间里
Stopwatch sw = new Stopwatch();
long num = 0;
sw.Reset();
sw = Stopwatch.StartNew();
for (int i = 1; i < 100000000; i++)
{
num += 1;
}
sw.Stop();
TimeSpan el = sw.Elapsed;
Console.WriteLine("花费 {0} ", el);
long ms = sw.ElapsedMilliseconds;
Console.WriteLine("花费 {0} 毫秒", ms);
long tk = sw.ElapsedTicks;
Console.WriteLine("花费 {0} ticks", tk);