Skip to main content
gamedev.net
🔒 Locked

.net timer class?

Started by nagromo Mar 23, 2004 at 6:55 PM 4 replies 450+ views
Original Post
nagromo
nagromo
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!
wyrd
wyrd
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).
edotorpedo
edotorpedo

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
Edo
Arild Fines
Arild Fines
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]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Fidelio66
Fidelio66
There are 3 timers in .NET, this article compares them.

[edited by - Fidelio66 on March 24, 2004 9:49:31 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.