[C#/.NET]C#&.NET/.NET Core应用程序中如何计算某个方法的执行耗时/花费时间?
2.09K 次浏览
2 个回答
-
或者,你也可以
new
来创建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(); TimeSpan ts = stopWatch.Elapsed; 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); } }