using System.Runtime.InteropServices; public class HighResolutionTimer { [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 stopTime; private long frequency; public HighResolutionTimer() { QueryPerformanceFrequency(out frequency); } public void Start() { QueryPerformanceCounter(out startTime); } public void Stop() { QueryPerformanceCounter(out stopTime); } public double Duration() { return (double)(stopTime - startTime) / (double)frequency; } }