#region Copyright(c) Travis Robinson
#endregion
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Win32
{
internal class HiPerfTimer
{
#region Public Members
public HiPerfTimer()
{
startTime = 0;
stopTime = 0;
length = 0;
if (QueryPerformanceFrequency(out freq) == false)
{
throw new Win32Exception();
}
}
public double Bps
{
get
{
double duration = Duration;
if (duration.Equals(0.0)) return 0.0;
return length/duration;
}
}
public double Duration
{
get
{
return (stopTime - startTime)/(double) freq;
}
}
public long Ticks
{
get
{
long tDiff = stopTime - startTime;
tDiff = (tDiff*10000000)/freq;
return tDiff;
}
}
public void Start()
{
length = 0;
QueryPerformanceCounter(out startTime);
}
public void Stop()
{
QueryPerformanceCounter(out stopTime);
}
public void Stop(int addLength)
{
QueryPerformanceCounter(out stopTime);
length += addLength;
}
#endregion
#region Private Members
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
private readonly long freq;
private long length;
private long startTime, stopTime;
#endregion
}
}