計算方法またはモジュールの実行時間

8024 ワード

/// <summary>

///  

/// </summary>

public class QueryPerformance

{

    [DllImport("Kernel32.dll")]

    private static extern bool QueryPerformanceCounter(out long performanceCount);



    [DllImport("Kernel32.dll")]

    private static extern bool QueryPerformanceFrequency(out long frequency);



    private long begintTime = 0;//   



    private long endTime = 0;//   



    private long frequency = 0;//   

    /// <summary>

    ///  

    /// </summary>

    public long BegintTime

    {

        get { return begintTime; }

    }

    /// <summary>

    ///  

    /// </summary>

    public long EndTime

    {

        get { return endTime; }

    }

    /// <summary>

    /// CPU 

    /// </summary>

    public long Frequency

    {

        get { return frequency; }

    }

    /// <summary>

    ///  , CPU 

    /// </summary>

    public QueryPerformance()

    {

        QueryPerformanceFrequency(out frequency);//   

    }

    /// <summary>

    ///  

    /// </summary>

    public void Start()

    {

        QueryPerformanceCounter(out begintTime);

    }

    /// <summary>

    ///  

    /// </summary>

    /// <param name="showRecord"> </param>

    /// <returns> </returns>

    public double Stop(bool showRecord)

    {

        QueryPerformanceCounter(out endTime);



        if (showRecord)

        {

            return TastTime;

        }

        return 0.0D;

    }

    /// <summary>

    ///  

    /// </summary>

    public double TastTime// : S  

    {

        get

        {

            if (frequency > 0)

                return (double)(endTime - begintTime) / frequency;

            else

                return 0;

        }

    }

}

次に使用方法を示します.
QueryPerformance queryPerformance = new QueryPerformance();

queryPerformance.Start();  



// 



double time = queryPerformance.Stop(true);