• Advertisement

Archived

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

.NET high resolution timer?

This topic is 5409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been looking for a native .NET high resolution timer, something more percise than Environment.TickCount. Thanks for any info.

Share this post


Link to post
Share on other sites
Advertisement
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

  • Advertisement