I usually use a stopwatch, but I also used p / invoke for kernel32.dll:
[DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter(out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency(out long lpFrequency);
I have two methods: Start () and Stop ():
public void Start() { Thread.Sleep(0); QueryPerformanceCounter(out startTime); } public void Stop() { QueryPerformanceCounter(out stopTime); }
To request the duration between calls in Start () and Stop ():
public double Duration { get { return (double)(stopTime - startTime)*1000 / (double)freq; } }
Note that freq is defined in the constructor:
public PerformanceTimer() { startTime = 0; stopTime = 0; if (QueryPerformanceFrequency(out freq) == false) {
For my needs, I did not notice a big difference, although I suggest you try and see what suits you.
Edit: I was able to find the source code from MSDN . It seems that there are some improvements made with .Net 1.1. A delivery message is what Microsoft claims this method provides nanosecond accuracy.
Charlie salts
source share