使用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