使用StopWatch类,可以计时,需引入System.Diagnostics 命名空间。
static void Main(string[] args) { Console.WriteLine("Main Start...."); Stopwatch sw = new Stopwatch(); sw.Start(); Thread.Sleep(3000); sw.Stop(); Console.WriteLine("time elapsed "+sw.ElapsedMilliseconds); sw.Restart(); //置0,并重新开始计时, 如果直接使用Start(),必须先调用Reset()置0 Thread.Sleep(3000); sw.Stop(); Console.WriteLine("Main end...." + sw.Elapsed); Console.ReadLine(); }
运行结果:
Main Start....
time elapsed 3004
Main end....00:00:03.0093181