SRL Archive - Help and Support

Why is this function so slow?


NCDS

2009-12-22 23:37:06

I don't see how it takes so long..


var
FindTree_ArrCnt, TreeColors: array of Integer;

function N_PointToBox(Point: TPoint; W, H: Integer): TBox;
var
a, b: Integer;
begin
a := W / 2;
b := H / 2;
with Result do
begin
x1 := Point.x - a;
y1 := Point.y - b;
x2 := Point.x + a;
y2 := Point.y + b;
end;
end;

function FindTree(Clrs: TIntegerArray): TBox;
var
Arr: array of TPoint; i, t: Integer;
begin
if not LoggedIn then
Exit;
MarkTime(t);

for i := 0 to High(Clrs) do
begin
FindColorsSpiralTolerance(MSCx, MSCy, Arr, Clrs[i], MSx1, MSy1,
MSx2, MSy2, 2);
if Length(Arr) > 0 then
Break;
if i = High(Clrs) then
begin
Writeln('Failed to find tree''s.');
Exit;
end;
end;

SortTPAFrom(Arr, Point(MSCx, MSCy));
i := RandomRange(FindTree_ArrCnt[0], FindTree_ArrCnt[1]);
Result := N_PointToBox(Point(Arr[i].x, Arr[i].y), 20, 20);

//Debug
Writeln('Found tree in '+IntToStr(t)+' ms.');
Writeln('Chose point '+IntToStr(i)+' in array.');
Writeln('Found tree at: '+IntToStr(Arr[i].x)+', '+IntToStr(Arr[i].y)+';');
end;

begin
SetUpSRL;
ActivateClient;
FindTree_ArrCnt := [3, 7];
TreeColors := [10220279, 9956593, 14021108];
FindTree(TreeColors);
end;

SRL Compiled in 16 msec
Found tree in 21528499 ms.

Wanted

2009-12-22 23:41:14

  for i := 0 to High(Clrs) do
begin
FindColorsSpiralTolerance(MSCx, MSCy, Arr, Clrs[i], MSx1, MSy1,
MSx2, MSy2, 2);
if Length(Arr) > 0 then
Break;
if i = High(Clrs) then
begin
Writeln('Failed to find tree''s.');
Exit;
end;
end;

You call FindColorsSpiralTolerance constantly… try adding a small wait.

NCDS

2009-12-22 23:46:43

From: IceFire908
  for i := 0 to High(Clrs) do
begin
FindColorsSpiralTolerance(MSCx, MSCy, Arr, Clrs[i], MSx1, MSy1,
MSx2, MSy2, 2);
if Length(Arr) > 0 then
Break;
if i = High(Clrs) then
begin
Writeln('Failed to find tree''s.');
Exit;
end;
end;

You call FindColorsSpiralTolerance constantly… try adding a small wait.


Even with a 200ms wait right after the FindColorsSpiralTolerance it still took:

Found tree in 22477352 ms.
:/

Edit: it find's the tree instantly though when I start the script, but say's it takes that long?

Wanted

2009-12-22 23:50:42

Lol, it's because you're using WriteLn(IntToStr(T));

when you should use IntToStr(TimeFromMark(T)) or GetSystemTime - T;

NCDS

2009-12-22 23:55:46

From: IceFire908
Lol, it's because you're using WriteLn(IntToStr(T));

when you should use IntToStr(TimeFromMark(T)) or GetSystemTime - T;



Found tree in 24 ms.
:duh:

Much better, thanks :)

Nava2

2009-12-23 23:35:40

Well if you are still worried, use shr 1 rather than / 2 and use a vaR to hold the height value. Also, calling consecutive FindColor* functiobs shouldn't compromise speed.

Wanted

2009-12-23 23:45:38

From: Nava2
Well if you are still worried, use shr 1 rather than / 2 and use a vaR to hold the height value. Also, calling consecutive FindColor* functiobs shouldn't compromise speed.




var
I, T, X: LongInt;

begin
T := GetSystemTime;
for I := 1 to 1000000 do
X := I shr 1;
WriteLn('shr ' + FloatToStr((GetSystemTime - T) / 1000000));
T := GetSystemTime;
for I := 1 to 1000000 do
X := I div 2;
WriteLn('div ' + FloatToStr((GetSystemTime - T) / 1000000));
T := GetSystemTime;
for I := 1 to 1000000 do
X := I / 2;
WriteLn('/ ' + FloatToStr((GetSystemTime - T) / 1000000));
end.
Successfully compiled (78 ms)
shr 0.003125
div 0.003094
/ 0.003141
Successfully executed


are bitwise really faster.. in scar atleast? I can never get good consistency :(

NCDS

2009-12-24 00:01:55

Thanks for the suggestion's. I may use them at a later date but I'm not too worried about it right now.
I got it down to 13 ms :), and I'm content with that for this..