C# High Resolution Timer

Started by
10 comments, last by Slug 19 years, 3 months ago
Sorry if this has been asked before but the search feature is down. Are there any built-in high resolution timers for C# or is importing QueryPerformanceCounter (and related functions) the way to go?
Advertisement
Debug.Stopwatch Although only available in .NET 2.0
QPC is the way to go for now.


Note: The 2.0 class is System.Diagnostics.Stopwatch

http://longhorn.msdn.microsoft.com/?//longhorn.msdn.microsoft.com/lhsdk/ref/ns/System.Diagnostics/c/Stopwatch/Stopwatch.aspx
________________________________________________Chris McGuirkLead Programmer - Axiom 3D EngineC# gaming, the way of the future!http://www.axiom3d.org
There's one built into your CPU!
The Read Time Stamp Counter (RDTSC) Assembly instruction will increment every clock cycle...
Killers don't end up in jailThey end up on a high-score!
@nife: Do you know some secret way of including inline assembly in a C# program that you're not telling us about?

The only way I know is to build it in unsafe C++ and importing it, which is a pretty annoying development path -- not to mention your entire assembly is then unsafe.
enum Bool { True, False, FileNotFound };
Can't you just link an obj file already compiled with NASM or MASM?

Who cares if a couple of instructions is unsafe (well, I guess you do ;-))? Just calling the RDTSC and returning in EAX shouldn't be a risk in my opinion...
Killers don't end up in jailThey end up on a high-score!
Quote:Original post by nife
Can't you just link an obj file already compiled with NASM or MASM?

Who cares if a couple of instructions is unsafe (well, I guess you do ;-))? Just calling the RDTSC and returning in EAX shouldn't be a risk in my opinion...


I'm afraid you've missed the point. In .NET land, there are two kinds of assemblies. Safe assemblies and unsafe assemblies. Even though your assembly instructions are entirely harmless in this case, the .NET Runtime can't distinguish between it and malicious or buggy code that could do harmfull things. Therefore, it will require the end users to increase the program's security permissions just so the program can check the time.

EDIT: Corrected typo.
There is a timer in the DX SDK that may help, have a look there.
Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
Wait... If I import QPC and related functions from a DLL my program is considered unsafe?

And Mykre, I can't seem to find it. Any idea what the sample is called?
Here is what i use:

using System;using System.Runtime.InteropServices;using System.ComponentModel;using System.Threading;namespace SysUtil{	/// <summary>	/// Summary description for HiPerfTimer.	/// </summary>	public class HiPerfTimer	{		[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, stopTime;		private long freq;		public HiPerfTimer()		{			startTime = 0;			stopTime  = 0;			if (QueryPerformanceFrequency(out freq) == false)			{				// high-performance counter not supported				throw new Win32Exception();			}		}		// Start the timer		public void Start()		{			// lets do the waiting threads there work			Thread.Sleep(0);			QueryPerformanceCounter(out startTime);		}		// Stop the timer		public void Stop()		{			QueryPerformanceCounter(out stopTime);		}		public long GetFreq()		{			return freq;		}		public long Query()		{			long time;			QueryPerformanceCounter(out time);			return time;		}		// Returns the duration of the timer (in seconds)		public double Duration		{			get			{				return (double)(stopTime - startTime) / (double) freq;			}		}	}}

SynexCode Monkey

This topic is closed to new replies.

Advertisement