簡體   English   中英

衡量代碼在Visual Studio 2010 IDE中運行所需時間的工具

[英]Tool to measure the time taken for a code to run in visual studio 2010 ide

我有沒有什么工具可以用來衡量功能或部分代碼在Visual Studio 2010中執行所需的時間。

謝謝你的幫助

您不需要工具,而是可以使用System.Diagnostics.Stopwatch

提供一組方法和屬性,可用於准確測量經過的時間。

秒表通過計算基礎計時器機制中的計時器滴答來測量經過的時間。 如果安裝的硬件和操作系統支持高分辨率性能計數器,則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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM