Levon Chen 的脚步

生活在于拥抱改变。

C# 对某一部分代码执行时间进行检测

  sealed class CommandTimer : IDisposable
  {
   Stopwatch commandStopwatch = new Stopwatch();

   internal CommandTimer(string command)
   {
    commandStopwatch.Start();
   }

   public void Dispose()
   {
    commandStopwatch.Stop();
    Console.WriteLine("\nCommand elapsed time: {0} sec", commandStopwatch.ElapsedMilliseconds / 1000f);
   }
  }

 

使用如下:

    using (CommandTimer t = new CommandTimer("-----"))
    {
    。。。。。。。。。。。。。。
    }

posted on 2010-09-01 13:46  Levon Chen  阅读(293)  评论(0编辑  收藏  举报

导航