uses Math;
procedure TForm1.Button1Click(Sender: TObject);
const MinInt = Low(Integer);
var a:
Array of Integer;
i, min, max: Integer;
p: PInteger;
q1, q2, f: Int64;
s:
String;
begin
SetLength(a, $10ffffff);
// $1ffffffc dürfte etwa das Maximum von rund 2 GB sein.
// Hab hier das Programm allerdings nur im 2 GB-Mode laufen, weswegen
// ich nicht an 2 GB rankomm, da die DLLs + EXE + MemoryManager ja
// einen Teil schon belegt haben.
For i := 0
to High(a)
do a[i] := Random(MaxInt);
QueryPerformanceFrequency(f);
s := '
';
QueryPerformanceCounter(q1);
min := MinIntValue(a);
max := MaxIntValue(a);
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 1: %f ms', [s, (q2 - q1) * 1000 / f]);
QueryPerformanceCounter(q1);
min := a[0];
max := a[0];
For i := 1
to High(a)
do Begin
If a[i] < min
Then min := a[i];
If a[i] > max
Then max := a[i];
End;
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 2: %f ms', [s, (q2 - q1) * 1000 / f]);
QueryPerformanceCounter(q1);
min := MaxInt;
max := MinInt;
For i := 0
to High(a)
do
If a[i] < min
Then min := a[i]
Else If a[i] > max
Then max := a[i];
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 3: %f ms', [s, (q2 - q1) * 1000 / f]);
QueryPerformanceCounter(q1);
min := MaxInt;
max := MinInt;
P := Pointer(a);
For i := High(a)
downto 0
do Begin
If P^ < min
Then min := P^
Else If P^ > max
Then max := P^;
Inc(P);
End;
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 4: %f ms', [s, (q2 - q1) * 1000 / f]);
QueryPerformanceCounter(q1);
min := MaxInt;
max := MinInt;
P := Pointer(a);
For i := High(a)
downto 0
do Begin
If P^ < min
Then min := P^;
Inc(P);
End;
P := Pointer(a);
For i := High(a)
downto 0
do Begin
If P^ > max
Then max := P^;
Inc(P);
End;
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 5: %f ms', [s, (q2 - q1) * 1000 / f]);
QueryPerformanceCounter(q1);
min := MaxInt;
max := MinInt;
P := Pointer(a);
For i := High(a)
downto 0
do Begin
If P^ < min
Then Begin
min := P^;
If min = MinInt
Then Break;
End;
Inc(P);
End;
P := Pointer(a);
For i := High(a)
downto 0
do Begin
If P^ > max
Then Begin
max := P^;
If max = MaxInt
Then Break;
End;
Inc(P);
End;
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 6: %f ms', [s, (q2 - q1) * 1000 / f]);
QueryPerformanceCounter(q1);
ASM
PUSH EDI
PUSH EBX
MOV EAX, &MinInt
MOV EDX, &MaxInt
MOV EDI, &a
TEST EDI, EDI
JZ @@none
MOV ECX, [EDI - 4]
@@loop:
MOV EBX, [EDI + ECX * 4]
CMP EAX, EBX
JGE @@greater
MOV EAX, EBX
@@greater:
CMP EDX, EBX
JLE @@lower
MOV EDX, EBX
@@lower:
LOOP @@loop
@@none:
MOV &min, EAX
MOV &max, EDX
POP EBX
POP EDI
End;
QueryPerformanceCounter(q2);
If min = max
Then ;
s := Format('
%s - 7: %f ms', [s, (q2 - q1) * 1000 / f]);
ShowMessageFmt('
%d : %d MB %s', [Length(a), (Length(a) * SizeOf(Integer))
shr 20, s]);
end;