现在的位置: 首页 > 综合 > 正文

测试某个方法的执行时间

2012年11月24日 ⁄ 综合 ⁄ 共 393字 ⁄ 字号 评论关闭

    有时,我们需要知道一个方法的具体执行时间,需要用到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(),"时间统计");

抱歉!评论已关闭.