Archived

This topic is now archived and is closed to further replies.

FieroAddict

.NET high resolution timer?

Recommended Posts

P/Invoke QueryPerformanceCounter and QueryPerformanceFrequency, something like this:


  
// $Id: Timer.cs 411 2003-04-19 00:20:22Z Arild $

using System;
using System.Runtime.InteropServices;

namespace Utils
{
/// <summary>

/// A high precision timer for performance measurement purposes.

/// </summary>

public class Timer
{
public Timer()
{
this.startTime = 0;
this.endTime = 0;

QueryPerformanceFrequency( out this.frequency );
}


public void Start()
{
QueryPerformanceCounter( out this.startTime );
}

public void End()
{
QueryPerformanceCounter( out this.endTime );
}

public double Interval
{
get{ return (double)(this.endTime-this.startTime)/(double)frequency; }
}


[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 endTime;
private long frequency;
}
}




"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
-- Cardinal Bellarmine

Share this post


Link to post
Share on other sites