Results 1 to 13 of 13

Thread: Which is more efficient (faster)? Arrays or Constants!

  1. #1
    Join Date
    May 2008
    Posts
    1,345
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Which is more efficient (faster)? Arrays or Constants!

    We had a small argument on the irc, and I decided to test it. The test is the same. Edited Typesend to remove as much possibility of randomization as possible, the EXACT same lines, everything. Here's what I got:

    I ran the constant version three times. Average of 2614 milliseconds to type three lines.

    I ran the arrays version three times. Average of 2672 milliseconds to type three lines.


    Interesting isn't it? Try it yourself, post YOUR results:

    Constant script:

    SCAR Code:
    Program New;
    {.include srl/srl.scar}

    Const
     Type1 = 'Hi there';
     Type2 = 'Lolwut';
     Type3 = 'Hey there';
     
    Var
      Mark : Integer;

    procedure TypeSend2(Text: string);
    var
      S: string;
      I: Integer;
      C: Byte;
      Shift: Boolean;
    begin
      S:= 'ABCDEFGHIJKLMNOPQRSTUVWXZ' + '~!@#$%^&*()_+{}|:"<>?';
      for I:= 1 to Length(Text) do
      begin
        Shift:= (Pos(Text[i], S) > 0);
        if(Shift)then
        begin
          KeyDown(VK_SHIFT) Wait(40);
          while(Pos(Text[i], S) > 0)and(I <= Length(Text))do
          begin
            C := GetKeyCode(StrGet(Text, I));
            TypeByte(c);
            I:= I + 1;
            if(I > Length(Text))then Break;
          end;
        end;
        if(Shift)then
          KeyUp(VK_SHIFT);
        Wait(40);
        if(I <= Length(Text))then
        begin
          C:= GetKeyCode(StrGet(Text, I));
          TypeByte(C);
          Wait(40);
        end;
      end;
      C := GetKeyCode(Chr(13));
      TypeByte(C);
    end;

    Procedure Speak;
    Begin
      TypeSend2(Type1);
      TypeSend2(Type2);
      TypeSend2(Type3);
    End;

    Begin
      SetupSrl;
      Marktime(mark)
      Speak;
      WriteLn(IntToStr(TimeFromMark(Mark)));
    End.

    Array Script:

    SCAR Code:
    Program New;
    {.include srl/srl.scar}

    Var
      Arrays : Array[0..2] Of String;
      Mark, I : Integer;

    procedure TypeSend2(Text: string);
    var
      S: string;
      I: Integer;
      C: Byte;
      Shift: Boolean;
    begin
      S:= 'ABCDEFGHIJKLMNOPQRSTUVWXZ' + '~!@#$%^&*()_+{}|:"<>?';
      for I:= 1 to Length(Text) do
      begin
        Shift:= (Pos(Text[i], S) > 0);
        if(Shift)then
        begin
          KeyDown(VK_SHIFT) Wait(40);
          while(Pos(Text[i], S) > 0)and(I <= Length(Text))do
          begin
            C := GetKeyCode(StrGet(Text, I));
            TypeByte(c);
            I:= I + 1;
            if(I > Length(Text))then Break;
          end;
        end;
        if(Shift)then
          KeyUp(VK_SHIFT);
        Wait(40);
        if(I <= Length(Text))then
        begin
          C:= GetKeyCode(StrGet(Text, I));
          TypeByte(C);
          Wait(40);
        end;
      end;
      C := GetKeyCode(Chr(13));
      TypeByte(C);
    end;

    Procedure Speaks;
    Begin
      For I:=0 To 2 Do
        TypeSend2(Arrays[I]);
    End;

    Procedure DeclareArray123;
    Begin
      Arrays[0] := 'Hi there';
      Arrays[1] := 'Lolwut';
      Arrays[2] := 'Hey there';
    End;

    Begin
      SetupSrl;
      Marktime(mark);
      DeclareArray123;
      Speaks;
      WriteLn(IntToStr(TimeFromMark(Mark)));
    End.

  2. #2
    Join Date
    Apr 2007
    Posts
    2,593
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Constants, ftw.

  3. #3
    Join Date
    Feb 2007
    Location
    Alberta,Canada
    Posts
    2,358
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    arrays FTW!

    you should make this a poll.
    “Ignorance, the root and the stem of every evil.”

  4. #4
    Join Date
    Jun 2008
    Location
    San Diego, California
    Posts
    276
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Personally for me, It depends on what I'm doing.

    -The_Shermanator
    Current Project: All In 1 Falador Script - 20% DONE

  5. #5
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    I tested both and arrays were faster.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  6. #6
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    Quote Originally Posted by Dan's The Man View Post
    I tested both and arrays were faster.
    And my results were the opposite. Meaning: it doesn't really matter, both have their uses.

    Why the heck was this tested with SendKeys, also?
    :-)

  7. #7
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    arrays ftw...arrays you can edit whenever you want and they are dynamic so you can have a few lines in the var declaration and you can create a for loop etc to change them.

  8. #8
    Join Date
    Jul 2007
    Location
    Norway.
    Posts
    1,938
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    Let's take a look at TypeByte, shall we?

    SCAR Code:
    procedure TypeByte(k: Byte);
    begin
      KeyDown(k);
      Wait(10 + Random(50)); // <- ?
      KeyUp(k);
    end;

    Removed all randomness, you said?



    Edit: Why can't anybody see that this is the reason for why people are experiencing the difference? (If I'm right, that is. But I'm pretty sure I am.)

  9. #9
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    They should be about the same speed (which I believe your results are validating). You are basically testing an array vs an array. The only difference is one has been marked "DO NOT CHANGE" by the compiler (actually, constants can be changed, the compiler just tries to prevent it at compile time).

    You might as well make another test. Which is faster?

    var
    a, b : integer;
    a := a + 1; // <- This?
    b := b + 1; // <- or this?

  10. #10
    Join Date
    Jul 2008
    Posts
    907
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    they are similar, about 50 ms one way or another, but i prefer arrays anyway.


  11. #11
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    You should basically stop to try and get such a speed in Pascal Script, it is very slow, and you can get unexpected results when trying to optimize. It is an interpreter, after all...



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  12. #12
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    You should basically stop to try and get such a speed in Pascal Script, it is very slow, and you can get unexpected results when trying to optimize. It is an interpreter, after all...
    Disagree. Because it's so slow you NEED to optimize it as much as possible.
    Verrekte Koekwous

  13. #13
    Join Date
    May 2008
    Posts
    1,345
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by EvilChicken! View Post
    Let's take a look at TypeByte, shall we?

    SCAR Code:
    procedure TypeByte(k: Byte);
    begin
      KeyDown(k);
      Wait(10 + Random(50)); // <- ?
      KeyUp(k);
    end;

    Removed all randomness, you said?



    Edit: Why can't anybody see that this is the reason for why people are experiencing the difference? (If I'm right, that is. But I'm pretty sure I am.)
    /Facepalm.

    *Strolls on over to my Biggest screw ups, fails, etc. post*

    I completely forgot about that. Woops :/.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Arrays, stuck on arrays
    By Camaro' in forum OSR Help
    Replies: 1
    Last Post: 03-08-2008, 02:02 AM
  2. Efficient Utilization of Run Energy in Scripts.
    By L3ss Than 33 in forum Bot Information and Spottings
    Replies: 8
    Last Post: 02-03-2008, 09:56 AM
  3. Is this Efficient?
    By osmm in forum OSR Help
    Replies: 3
    Last Post: 12-26-2007, 11:56 PM

Posting Permissions

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