using System; using System.Runtime.InteropServices; namespace ExceptionPerformance { class PerfCounter { [DllImport("kernel32.dll")] extern static short QueryPerformanceCounter(ref long x); [DllImport("kernel32.dll")] extern static short QueryPerformanceFrequency(ref long x); private long m_frequency; private long m_start; private long m_stop; public PerfCounter() { QueryPerformanceFrequency(ref m_frequency); } public void Start() { m_start = GetCurrentValue(); } public void Stop() { m_stop = GetCurrentValue(); } public double GetElapsedTimeInSeconds() { return (double)(m_stop - m_start) * (1.0 / m_frequency); } public double GetElapsedTimeInMilliseconds() { return (double)(m_stop - m_start) * (1000.0 / m_frequency); } private long GetCurrentValue() { long time = 0; QueryPerformanceCounter(ref time); return time; } } }