Stopwatch可以测量一段程序的耗时
命名空间:using System.Diagnostics;
示例:
Stopwatch watch = Stopwatch.StartNew();//初始化watch.Start(); //启动计时器for(int i = 0; i <= 10; i++){Console.Write(i + \" \");Thread.Sleep(100);}watch.Stop(); //停止计时器Console.WriteLine();//Stopwatch类提供的提取时间的两种方法:Console.WriteLine(\"用时:\"+watch.Elapsed);Console.WriteLine(\"用时:\" + watch.ElapsedMilliseconds+\"毫秒\");/* 运行结果0 1 2 3 4 5 6 7 8 9 10用时:00:00:01.1110826 (返回TimeSpan类型)用时:1111毫秒 (返回long 类型)