How to create a high resolution timer (Views: 28)
Problem/Question/Abstract: How to create a high resolution timer Answer: Windows is not a real time operating system so it is not really able to reliably achieve high accuracy timing without using a device driver. The best I have been able to get is a few nanoseconds using QueryPerformanceCounter. This is the procedure I use: var WaitCal: Int64; procedure Wait(ns: Integer); var Counter, Freq, WaitUntil: Int64; begin if QueryPerformanceCounter(Counter) then begin QueryPerformanceFrequency(Freq); WaitUntil := Counter + WaitCal + (ns * (Freq div 1000000)); while Counter < WaitUntil do QueryPerformanceCounter(Counter); end else Sleep(ns div 1000); end; To get improved accuracy do this a little while before using Wait() var Start, Finish: Int64; Application.ProcessMessages; Sleep(10); QueryPerformanceCounter(Start); Wait(0); QueryPerformanceCounter(Finish); WaitCal := Start - Finish; A trick I have found to increase the reliability of this on my computer is to call Wait like this: Application.ProcessMessages; Sleep(0); DoSomething; Wait(10); DoSomethingElse; |