# Thread: Shortening while..do loops

1. ## Shortening while..do loops

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)
SCAR Code:
{*******************************************************************************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.
SCAR Code:
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:
SCAR Code:
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.
Last edited by NCDS; 02-16-2010 at 07:31 PM.

2. 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)
Last edited by anonymity; 02-16-2010 at 06:10 PM.

3. Originally Posted by 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.

4. 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

5. Originally Posted by 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
Last edited by NCDS; 02-16-2010 at 07:35 PM.