Mohamed Mansour's Personal Website
Articles
High Resolution Timer in NET 2.0
Posted on December 21, 2006, 9:11 pm EST
The Classic
Windows Forms Timer in .NET 2.0 is a bit slow, one may want to use the highest resolution timer the system has. This library will use the fastest Windows Timer by interoping the Kernel32.dll inorder to access the QueryPerfomanceCounter.
Comparisons between existing timers
CODE:
Function Units Resolution
---------------------------------------------------------------------------
Now, Time, Timer seconds 1 second
GetTickCount milliseconds approx. 10 ms
TimeGetTime milliseconds approx. 10 ms
QueryPerformanceCounter QueryPerformanceFrequency same
The QueryPerformanceCounter function returns the current value of the high-resolution performance counter. The current Windows Forms Timer, returns a 1 second resolution, in many cases that may be enough. When timing is a must, using the QueryPerformanceFrequency is the best in its class.
How to use
Import the namespace
CODE:using HighResolutionTimer;
We need to create an instance of the HighResolutionTimer
CODE:SystemTimer timer = new SystemTimer();
Then if you wish to time a method or anything else you coudl do the following
CODE:
// Start the Timer
timer.Start();
// Lets do some stuff
int i;
for (i=0;i<1000;i++)
{
Console.Write(i + "|");
}
// Stop the Timer
timer.Stop();
Console.WriteLine("Duration: {0}",timer.Duration);
Source
CODE:
///
///
/// Mohamed Mansour 2006
/// http://www.m0interactive.com
///
///
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
namespace HighResolutionTimer
{
/// <summary>
/// Hi Performance Timer that works deep deep
/// inside the system timer...
/// Since Overhead of API calls is roughly 19ms
/// This is accounted for in this class
/// </summary>
public class SystemTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
// For Timing Duration purposes
private long start;
private long stop;
private long freq;
private long overhead;
// Used for Instant Time
private long temp;
// Constructor
public SystemTimer()
{
start = 0;
temp = 0;
stop = 0;
freq = this.Frequency;
Start();
Stop();
overhead = stop - start;
}
/// <summary>
/// Start The Timer
/// </summary>
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out start);
}
/// <summary>
/// Stop the timer
/// </summary>
public void Stop()
{
QueryPerformanceCounter(out stop);
}
/// <summary>
/// Returns the INSTANT time of system
/// within 10ms resolution
/// </summary>
public double InstantCounter
{
get
{
QueryPerformanceCounter(out temp);
return (double)(temp - start - overhead) / (double)freq;
}
}
/// <summary>
/// Returns the frequency
/// </summary>
public long Frequency
{
get
{
// Lets retrieve frequency
if (QueryPerformanceFrequency(out freq) == false)
{
// Not Supported
throw new Win32Exception();
}
return freq;
}
}
/// <summary>
/// Returns the duration of the timer
/// (in seconds)
/// </summary>
public double Duration
{
get
{
return (double)(stop - start - overhead) / (double)freq;
}
}
}
}
Download
The Library: here
The Source Code: here

