Results 1 to 5 of 5

Thread: Shortening while..do loops

  1. #1
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default 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. #2
    Join Date
    Jan 2007
    Location
    the middle of know-where
    Posts
    1,308
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    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.
    On vacation in NeverLand,
    Code:
    typedef int bool;
    enum { false, true };

  3. #3
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    Quote Originally Posted by anonymity View Post
    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. #4
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,488
    Mentioned
    3 Post(s)
    Quoted
    12 Post(s)

    Default

    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 & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  5. #5
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    Quote Originally Posted by Rasta Magician View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •