高性能 计时器:统计功能耗时

/// <summary>
/// 高性能 计时器:统计功能耗时ms
/// </summary>
public class HiPerfTimer
{
    [System.Security.SuppressUnmanagedCodeSecurity]
    [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
    [System.Security.SuppressUnmanagedCodeSecurity]
    [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(out long lpFrequency);
    private long startTime; // start time, in cpu ticks
    private long stopTime;  // stop time, in cpu ticks
    private long freq;      // CPU frequency, ticks per second
    public HiPerfTimer()
    {
        startTime = 0;
        stopTime = 0;
        if (false == QueryPerformanceFrequency(out freq))
        {
            throw new System.ComponentModel.Win32Exception(); // high-performance counter not supported
        }
    }
    public void Start()
    {
        System.Threading.Thread.Sleep(0);
        QueryPerformanceCounter(out startTime);
    }
    public void Stop()
    {
        QueryPerformanceCounter(out stopTime);
    }
    /// <summary>
    /// 间隔(ms)
    /// </summary>
    public double Duration
    {
        get
        {
            return (double)(stopTime - startTime) / (double)freq * 1000;
        }
    }

}
原文地址:https://www.cnblogs.com/wesson2019-blog/p/13346513.html