1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| procedure TfrmWait.Button4Click(Sender: TObject);
const
D32 = $66;
type
CompStr = string[25];
{Comps have up to 18 digits, plus commas, and sign}
function CompToStr(N: comp): CompStr;
var
Low3: string[3];
N1: extended;
begin
if N < 0
then Result := '-' + CompToStr(-N)
else begin
N1 := N / 1000;
Str(Round(Frac(N1) * 1000), Low3);
N := Int(N1);
if N > 0
then begin
while Length(Low3) < 3 do Low3 := '0' + Low3;
Result := CompToStr(N) + ThousandSeparator + Low3;
end
else Result := Low3
end;
end;
function RDTSC: comp;
var
TimeStamp: record
case byte of
1: (Whole: comp);
2: (Lo, Hi: LongInt);
end;
begin
asm
db $0F; db $31; {BASM doesn't support RDTSC}
{Pentium RDTSC - Read Time Stamp Counter - instruction}
{$ifdef Cpu386}
mov [TimeStamp.Lo],eax // the low dword
mov [TimeStamp.Hi],edx // the high dword
{$else}
db D32
mov word ptr TimeStamp.Lo,AX
{mov [TimeStamp.Lo],eax - the low dword}
db D32
mov word ptr TimeStamp.Hi,DX
{mov [TimeStamp.Hi],edx - the high dword}
{$endif}
end;
Result := TimeStamp.Whole;
end;
var
ff,i1,i2: int64;
c1,c2: comp;
t1,t2: cardinal;
s: string;
begin
Beep;
// compteur haute précision
QueryPerformanceFrequency(ff); // fréquence du compteur
ShowMsg(IntToStr(ff)); // pour moi, environ 1,2 MHz : 1 193 180 s-1
QueryPerformanceCounter(i1); // résolution de 1/ff secondes (pour moi, environ 0,83 µs)
// compteur de cycles d'horloges des Pentium
// appel grâce aux fonctions de Jon Shemitz
// http://www.midnightbeach.com/rdtsc.html
c1:=RDTSC; // résolution d'1 cycle d'horloge (pour moi, environ 1/450 µs)
// compteur faible précision
t1:=GetTickCount; // résolution de l'ordre de la ms (mais pas précis en dessous de 10 ms)
// pause d'environ 1,5 s
Sleep(1500);
// arrêts des compteurs
QueryPerformanceCounter(i2);
c2:=RDTSC;
t2:=GetTickCount;
// compteur haute précision
ShowMsg(FloatToStr((i2-i1)/ff)); // 1,495567 s (1496 ms)
// compteur de cycles
ShowMsg(FloatToStr((c2-c1)/(i2-i1)*ff)); // donne la fréquence du processeur : 451 022 054 Hz (j'ai un PIII 450 MHz)
// Ticks
ShowMsg(IntToStr(t2-t1)); // j'obtiens 1500 ms
end; |
Partager