参考资料:https://docs.microsoft.com/zh-cn/dotnet/api/system.diagnostics.stopwatch?view=netframework-4.7.2&f1url=%3FappId%3DDev16IDEF1%26l%3DZH-CN%26k%3Dk(System.Diagnostics.Stopwatch);k(TargetFrameworkMoniker-.NETFramework,Version%253Dv4.7.2);k(DevLang-csharp)%26rd%3Dtrue
Stopwatch 类
定义命名空间:System.Diagnostics
程序集:System.dll
提供一组方法和属性,可用于准确地测量运行时间。
Stopwatch实例可以度量一个间隔的运行时间,或度量多个间隔内所用时间的总和。 在典型的 Stopwatch 方案中,调用 Start 方法,最后调用 Stop 方法,然后使用属性检查运行时间 Elapsed 。
// 使用 StopWatch 进行测量 using System; using System.Diagnostics; using System.Threading; class Program { static void Main(string[] args) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start();//开始或继续测量某个时间间隔的运行时间。
Thread.Sleep(10000);//模拟耗时操作
stopWatch.Stop();//停止测量某个时间间隔的运行时间。 // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; //获取当前实例测量得出的总运行时间。 // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours,
ts.Minutes,
ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); } }
浙公网安备 33010602011771号