.Net统计代码执行时间

在.NET中,使用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();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value. 
        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);
    }
}

需要更详细的介绍请参考:http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx
posted @ 2014-08-26 10:28  IronXman  阅读(447)  评论(0编辑  收藏  举报