计算 某段代码的运行时长
static void Main(string[] args) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start();//开启计时 for (int i = 0; i < 30; i++) { Thread.Sleep(100); //Console.WriteLine(i+); } sw.Stop();//结束计时 var s = sw.ElapsedMilliseconds; Console.WriteLine("耗时:" + s + "毫秒"); Console.ReadKey(); }
如上小demo,可以手动计算出耗时代码