纳秒级性能计时器

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;

namespace Masuit.Tools.Systems
{
///


/// 纳秒级计时器
///

public class HiPerfTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(out long lpFrequency);

    private long _startTime;
    private long _stopTime;
    private readonly long _freq;

    /// <summary>
    /// 纳秒计数器
    /// </summary>
    public HiPerfTimer()
    {
        _startTime = 0;
        _stopTime = 0;

        if (QueryPerformanceFrequency(out _freq) == false)
        {
            // 不支持高性能计数器 
            throw new Win32Exception();
        }
    }

    /// <summary>
    /// 开始计时器
    /// </summary>
    public void Start()
    {
        // 来让等待线程工作 
        Thread.Sleep(0);
        QueryPerformanceCounter(out _startTime);
    }

    /// <summary>
    /// 启动一个新的计时器
    /// </summary>
    /// <returns></returns>
    public static HiPerfTimer StartNew()
    {
        HiPerfTimer timer = new HiPerfTimer();
        timer.Start();
        return timer;
    }

    /// <summary>
    /// 停止计时器
    /// </summary>
    public void Stop()
    {
        QueryPerformanceCounter(out _stopTime);
    }

    /// <summary>
    /// 时器经过时间(单位:秒)
    /// </summary>
    public double Duration => (_stopTime - _startTime) / (double)_freq;

    /// <summary>
    /// 执行一个方法并测试执行时间
    /// </summary>
    /// <param name="action"></param>
    /// <returns></returns>
    public static double Execute(Action action)
    {
        var timer = new HiPerfTimer();
        timer.Start();
        action();
        timer.Stop();
        return timer.Duration;
    }
}

}

//调用
private void button2_Click(object sender, EventArgs e)
{
HiPerfTimer timer = HiPerfTimer.StartNew();//执行一个待办事项并测试执行时间
//待办事项
timer.Stop();
label1.Text="执行耗时" + timer.Duration + "s";// 返回测试时间
double time = HiPerfTimer.Execute(() =>//执行一个方法并测试执行时间
{
//方法
});
label1.Text="执行耗时" + time + "s";//返回测试时间
}

posted @ 2020-05-01 22:39  zhujie-  阅读(573)  评论(0编辑  收藏  举报