Mega Code Archive

 
Categories / Delphi / Examples
 

Queryperformancecounter[] vs gettickcount[]

QueryPerformanceCounter() uses the PC's clock counter just as GetTickCount(), but it reads the current value of the countdown register in the timer chip to gain more accuracy -- down to 1.193MHz (about 800ns). However, it takes 5 to 10us to call QueryPerformanceCounter, because it has to do several port I/O instructions to read this value. If you are running multiprocessor NT, QueryPerformanceCounter uses the Pentium cycle counter. You should not be using the raw value of QueryPerformanceCounter; you should always divide by QueryPerformanceFrequency to convert to some known time base. QueryPerformanceCounter doesn't run at the same rate on all machines. The example below shows how to use it in Delphi 3/4: type TInt64 = TLargeInteger; var Frequency, lpPerformanceCount1, lpPerformanceCount2 : TLargeInteger; begin QueryPerformanceCounter(TInt64((@lpPerformanceCount1)^)); // activity to measure my_proc; QueryPerformanceCounter(TInt64((@lpPerformanceCount2)^)); QueryPerformanceFrequency(TInt64((@Frequency)^)); // this shows the time in ns ShowMessage(IntToStr(Round(1000000 * (lpPerformanceCount2.QuadPart - lpPerformanceCount1.QuadPart) / Frequency.QuadPart))); end;