Jump to content
  • Advertisement
Sign in to follow this  
GroZZleR

C# High Resolution Timer

This topic is 4884 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

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Debug.Stopwatch Although only available in .NET 2.0

Share this post


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

Share this post


Link to post
Share on other sites
There's one built into your CPU!
The Read Time Stamp Counter (RDTSC) Assembly instruction will increment every clock cycle...

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!