代码
/*
Stopwatch 计算程序所有时间
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace 计算程序运行时间
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
//ArrayList aList = new ArrayList();
//for (int i = 0; i < 10000000; i++)
//{
// aList.Add(i);
//}
//00:00:00.8874700
List list = new List();
for (int i = 0; i < 10000000; i++)
{
list.Add(i);
}
//00:00:00.0930832
sw.Stop();
//运行时间
Console.WriteLine(sw.Elapsed);
Console.ReadKey();
}
}
}
Stopwatch属于命名空间System.Diagnostics,程序运行时间最终返回的是TimeSpan,该类封装了一些相关属性,例如时分秒毫秒等,可以在代码中自由调用。