Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
New related comments Number of comments in the last 48 hoursPlay WAV files 1 new comments
|
QueryPerformanceCounter() vs. GetTickCount()
This article has not been rated yet. After reading, feel free to leave comments and rate it.
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)^));
my_proc;
QueryPerformanceCounter(TInt64((@lpPerformanceCount2)^));
QueryPerformanceFrequency(TInt64((@Frequency)^));
ShowMessage(IntToStr(Round(1000000 * (lpPerformanceCount2.QuadPart -
lpPerformanceCount1.QuadPart) / Frequency.QuadPart)));
end; | |  | |  |
Comments:
|