|
// -------------------------------------------------------
// SqlBuilder by Elm�Soft
// www.netcult.ch/elmue
// www.codeproject.com/KB/database/SqlBuilder.aspx
// -------------------------------------------------------
using System;
using System.Runtime.InteropServices;
namespace SqlBuilder
{
/// <summary>
/// The performance counter is better than 1 ms while the Tickcount only increments approx every 15 ms
/// If the hardware supportes it, this class uses the more exact performance counter.
/// .NET does not support a performance counter. DateTime.Tick is not precise!
/// </summary>
public class Measure
{
[DllImport("kernel32.dll", EntryPoint="QueryPerformanceCounter")]
private static extern int QueryPerformanceCounter(out long s64_Counter);
[DllImport("kernel32.dll", EntryPoint="QueryPerformanceFrequency")]
private static extern int QueryPerformanceFrequency(out long s64_Frequency);
private static long s64_Frequency = 0;
private long s64_Start;
public Measure()
{
if (s64_Frequency == 0)
{
QueryPerformanceFrequency(out s64_Frequency);
s64_Frequency /= 1000;
}
Start();
}
/// <summary>
/// Start a new measurement
/// </summary>
public void Start()
{
if (s64_Frequency == 0)
s64_Start = Environment.TickCount;
else
QueryPerformanceCounter(out s64_Start);
}
/// <summary>
/// returns the time in ms which has elapsed since creating this class or calling Start()
/// </summary>
public int ElapsedTimeInt
{
get
{
if (s64_Frequency == 0) // hardware does not support Performance counters
{
return Environment.TickCount - (int)s64_Start;
}
else
{
long s64_Now;
QueryPerformanceCounter(out s64_Now);
return (int) ((s64_Now - s64_Start) / s64_Frequency);
}
}
}
public string ElapsedTimeStr
{
get
{
int s32_Elapsed = ElapsedTimeInt;
return string.Format("{0}.{1}", s32_Elapsed/1000, (s32_Elapsed%1000).ToString("000"));
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.