using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;
namespace WindowsFormsApplication4
{
class HiPerformanTimer
{
//引用Win32 API中的QueryPerformanceCounter()方法
//该方法用来查询任意时刻高精度计数器的实际值
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformaceCount); //引用Win32API中QueryPerformanceFrequency()方法
//该方法返回高精度计数器每秒的计数值
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFerquency(out long lpFrequency); private long startTime, stopTime;
private long frequency; public HiPerformanTimer()
{
this.startTime = 0;
this.stopTime = 0; if (false == QueryPerformanceFerquency(out frequency))
{
throw new Win32Exception();
}
} public void Start()
{
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
} public void Stop()
{
QueryPerformanceCounter(out stopTime);
} public double Duration
{
get
{
return (double)(stopTime - startTime) * 1000 / (double)frequency;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;
namespace WindowsFormsApplication4
{
class HiPerformanTimer
{
//引用Win32 API中的QueryPerformanceCounter()方法
//该方法用来查询任意时刻高精度计数器的实际值
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformaceCount); //引用Win32API中QueryPerformanceFrequency()方法
//该方法返回高精度计数器每秒的计数值
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFerquency(out long lpFrequency); private long startTime, stopTime;
private long frequency; public HiPerformanTimer()
{
this.startTime = 0;
this.stopTime = 0; if (false == QueryPerformanceFerquency(out frequency))
{
throw new Win32Exception();
}
} public void Start()
{
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
} public void Stop()
{
QueryPerformanceCounter(out stopTime);
} public double Duration
{
get
{
return (double)(stopTime - startTime) * 1000 / (double)frequency;
}
}
}
}