Archived

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

nagromo

.net timer class?

Recommended Posts

nagromo    676
I am writing a game with the .net framework and I need a different type of timing. The System.Timer namespace seems to only have a class to call a callback after a given time. I want to find the time my loop took to execute for animation, AI, etc. Is there a timer that is compatible with Mono that returns a number (# of seconds/milliseconds/ticks since the start of code/last time it was called)? Basicly I am looking for something in .net, not calling QueryPerformanceCounter from a dll. Any help is appreciated!

Share this post


Link to post
Share on other sites
wyrd    112
Unfortunately .NET has no built in High Resolution Timer. You''ll need to access the systems API and snag one from there (like people do on windows with QueryPerformanceTimer).

Share this post


Link to post
Share on other sites
edotorpedo    198

DateTime goes down to Milliseconds, so this might not be accurate enough. Alternatively, you could just use the Profiler, however if your asking for Mono I don''t know if your IDE supports that.

Edo

Share this post


Link to post
Share on other sites
Arild Fines    968
I stole this class from an Eric Gunnerson article:


// $Id: Timer.cs 682 2003-06-07 16:43: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
{
[System.Diagnostics.DebuggerStepThrough]
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;
}
}


--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites