SRL Archive - News and General

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


Sandstorm

2008-12-01 23:08:15

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:

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:

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.

TViYH

2008-12-01 23:13:13

Constants, ftw.

Blumblebee

2008-12-01 23:30:51

arrays FTW!

you should make this a poll.

The_Shermanator

2008-12-02 00:53:07

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

-The_Shermanator

Daniel

2008-12-02 01:07:08

I tested both and arrays were faster.

Method

2008-12-02 01:23:40

[QUOTE=Dan's The Man;507810]I tested both and arrays were faster.[/QUOTE]

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?

Da 0wner

2008-12-02 01:58:27

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.

EvilChicken!

2008-12-02 04:13:17

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

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

boberman

2008-12-02 09:26:41

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?

Magiic

2008-12-02 16:41:56

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

Wizzup?

2008-12-02 16:43:12

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… :)

mastaraymond

2008-12-02 16:50:18

From: Wizzup?
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.

Sandstorm

2008-12-02 17:27:29

From: EvilChicken!
Let's take a look at TypeByte, shall we?

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 :/.