测试某个方法的执行时间

    有时,我们需要知道一个方法的具体执行时间,需要用到StopWatch类。代码如下:

Stopwatch sw = new Stopwatch();
 
sw.Start();
//这里填写要执行的代码
 sw.Stop();
 
 StringBuilder str = new StringBuilder();
 str.Append("总运行时间:" + sw.Elapsed+"\r\n");
 str.Append("测量实例得出的总运行时间(毫秒为单位):" + sw.ElapsedMilliseconds + "\r\n");
 str.Append("总运行时间(计时器刻度标识):" + sw.ElapsedTicks + "\r\n");
 str.Append("计时器是否运行:" + sw.IsRunning.ToString() + "\r\n");
 
 MessageBox.Show(str.ToString(),"时间统计");
原文地址:https://www.cnblogs.com/xiaoxiangfeizi/p/2595856.html