C# High Resolution Timer
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?
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
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
There's one built into your CPU!
The Read Time Stamp Counter (RDTSC) Assembly instruction will increment every clock cycle...
The Read Time Stamp Counter (RDTSC) Assembly instruction will increment every clock cycle...
@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.
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.
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...
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...
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.
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?
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; } } }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement