一、第一种方法利用System.DateTime.Now
static void Main(string[] args)
{
DateTime beginTime = DateTime.Now; //获取开始时间
System.Threading.Thread.Sleep(5000); //延时5秒
DateTime endTime = DateTime.Now; //获取结束时间
TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数
//输出运行时间。
Console.WriteLine("程序的运行时间:{0} 秒", oTime.TotalSeconds);
Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);
Console.ReadLine();
}
二、用Stopwatch类(System.Diagnostics)
public static void SubTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
//Shuffle(a) is the function you want to test.
int[] a = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
Shuffle(a);
sw.Stop();
TimeSpan ts = sw.Elapsed;
Console.WriteLine("DateTime costed for Shuffle function is: {0}ms", ts.TotalMilliseconds);
}