SCAR Code:
program New;
const
MMCX = 574;
MMCY = 118;
function QueryPerformanceFrequency(out Frequency: Int64): LongBool; external 'QueryPerformanceFrequency@kernel32.dll stdcall';
function QueryPerformanceCounter(out Counter: Int64): LongBool; external 'QueryPerformanceCounter@kernel32.dll stdcall';
procedure MarkTime(var Time: Int64);
var
Freq: Int64;
begin
if QueryPerformanceFrequency(Freq) then
QueryPerformanceCounter(Time)
else
Time := GetTickCount;
end;
function TimeFromMark(Mark: Int64): Double;
var
Freq, Now: Int64;
begin
if QueryPerformanceFrequency(Freq) then
begin
QueryPerformanceCounter(Now);
Result := ((Now - Mark) / Freq) * 1000;
end
else
Result := (GetTickCount - Mark);
end;
var
ii, i, h, MaxDist, Dist, Index, x, y: Integer;
p: TPointArray;
t: Int64;
d: Double;
begin
SetLength(p, 10000);
h := High(p);
for i := 0 to h do
p[i] := Point(RandomRange(-20000, 20000), RandomRange(-20000, 20000));
MarkTime(t);
for ii := 1 to 50 do
begin
MaxDist := 0;
for i := 0 to h do
begin
Dist := Distance(p[i].x, p[i].y, MMCX, MMCY);
if (Dist > MaxDist) then
begin
Index := i;
MaxDist := Dist;
end;
end;
end;
d := TimeFromMark(t);
WriteLn(Format('Distance || Result: %d -- 50 times: %fms -- 1 time: %fms', [Index, d, d / 50.0]));
MarkTime(t);
for ii := 1 to 50 do
begin
MaxDist := 0;
for i := 0 to h do
begin
x := p[i].x - MMCX;
if (x < 0) then
x := -x;
y := p[i].y - MMCY;
if (y < 0) then
y := -y;
Dist := x + y;
if (Dist > MaxDist) then
begin
Index := i;
MaxDist := Dist;
end;
end;
end;
d := TimeFromMark(t);
WriteLn(Format('Abs(x) + Abs(y) || Result: %d -- 50 times: %fms -- 1 time: %fms', [Index, d, d / 50.0]));
MarkTime(t);
for ii := 1 to 50 do
begin
MaxDist := 0;
for i := 0 to h do
begin
x := p[i].x - MMCX;
y := p[i].y - MMCY;
Dist := x*x + y*y;
if (Dist > MaxDist) then
begin
Index := i;
MaxDist := Dist;
end;
end;
end;
d := TimeFromMark(t);
WriteLn(Format('Sqr(x) + Sqr(y) || Result: %d -- 50 times: %fms -- 1 time: %fms', [Index, d, d / 50.0]));
end.
It's not much, but Distance seems slower.