SRL Archive - SRL Suggestions

Shortening while..do loops


NCDS

2010-02-16 12:10:11

I've noticed when looking through the include, especially Timing.Scar, there are a lot of while..do loops that could be shortened. So I tested to see if my way of shortening them would make them quicker or not.

Example: (just an example)

{*******************************************************************************
Function WaitUpTextMulti(S: TStringArray; Time: integer): Boolean;
By: Marpis & N1ke!
Description: Waits for a TStringArray of UpText, returns true if found
*******************************************************************************}
Function WaitUpTextMulti(S: TStringArray; Time: Integer): Boolean;
var
T, i: Integer;
begin
MarkTime(i);
Result := false;
T := GetSystemTime + Time;
while (GetSystemTime < T) do
begin
if (IsUpTextMultiCustom(S)) then
begin
Result := True;
Writeln(IntToStr(TimeFromMark(i)));
Exit;
end;
Wait(20 + Random(20));
end;
Writeln(IntToStr(TimeFromMark(i)));
end;
That's what I used to test WaitUpTextMulti with. I added all the timing with 'i' so I could time it. It ended up outputting: 47;

Then I edited that function to a shorter version and tested to see how long it took.

Function N_WaitUpTextMulti(S: TStringArray; Time: Integer): Boolean;
var
T, i: Integer;
begin
MarkTime(i);
Result := false;
T := GetSystemTime + Time;
while (GetSystemTime < T) and (not Result) do
begin
Result := (IsUpTextMultiCustom(S));
if Result then begin Writeln(IntToStr(TimeFromMark(i))); Exit; end;
Wait(20 + Random(20));
end;
Writeln(IntToStr(TimeFromMark(i)));
end;
Keep in mind that anything to do with the var 'i' is only for testing purposes right now.
And this function output: 31; therefor being faster..right?

This would be the function without the timing stuff, just for reference:

Function N_WaitUpTextMulti(S: TStringArray; Time: Integer): Boolean;
var
T: Integer;
begin
Result := false;
T := GetSystemTime + Time;
while (GetSystemTime < T) and (not Result) do
begin
Result := (IsUpTextMultiCustom(S));
Wait(20 + Random(20));
end;
end;

There are many functions in Timing.scar that this pertains to, as well as many other ways to go about this.

Please feel free to comment as I'm curious as to what others think about this.

anonymity

2010-02-16 12:49:57

Personally, I like what you've done.
Might I suggest running multiple instances of both to truly get a speed range. Also, take out the wait… because with the waiting random you won't precisely know which one is faster… ;)

E: My thought on the wait was mainly for testing instances that returned true and false. (more broad range, gets a better idea)

NCDS

2010-02-16 13:06:49

From: anonymity
Personally, I like what you've done.
Might I suggest running multiple instances of both to truly get a speed range. Also, take out the wait… because with the waiting random you won't precisely know which one is faster… ;)


Thanks, and I have no problem testing more functions I just wanted to get some other thoughts on it before I did.
Also, assuming both functions Result := True on the first try, which it should, then the random wait is skipped over anyways, but I can remove it while testing.

Sir R. M8gic1an

2010-02-16 14:02:20

Uh, very small change… also, you are wasting time by checking for result twice.

Lastly, when testing something, you want to take the randomness out of it.

Either have While (not Result) or have if Result then exit

~RM

NCDS

2010-02-16 14:28:44

From: Rasta Magician
Uh, very small change… also, you are wasting time by checking for result twice.

Lastly, when testing something, you want to take the randomness out of it.

Either have While (not Result) or have if Result then exit

~RM


I only checked for Result twice because I was timing it so that it didn't have to wait. I thought that might cause some confusion. That line wouldn't be there though in the actual function.

I'll edit the first post.

E: and I understand it is a very small change, just thought I would bring it up for discussion :redface: