SRL Archive - Help

Am I misremembering?


Bixby Sayz

2011-06-22 20:27:51

I could swear I saw a post on here a short while ago along the lines of:
instead of using

Hivar := High(SomeArray);
for I := 0 to HiVar

use <something> instead.

Tried searching but came up empty. Starting to question whether I actually saw such a thing on here.

MylesMadness

2011-06-22 20:35:07

Are you saying that using something else would be faster or what? AFAIK that is the fastest and cleanest way to do…what ever it is you are doing.

Bixby Sayz

2011-06-22 20:44:13

That's what I thought. Must have dreamt I read something like that. Was sort of dozing in front of the computer at the time.

JPHamlett

2011-06-22 20:55:14

Was it


for i := 0 to high(Array) do
begin
code here
end;

Gu3st...

2011-06-22 20:55:38

for I := High(blah) downto 0 do
Is a bit cleaner in the sense that you'd require 1 less variable, but it'll loop through the loop from the highest index to the lowest, so you have to be careful about that.

The reason why this is better than say
for I := 0 to High(blah) do
Is because the variable you put after to is calculated every time the loop starts over, meaning if you put High(blah) in there, it will call High(blah) each and every time the loop starts over. If you put 0 there, it wont call any functions thus making it faster and cleaner.

i luffs yeww

2011-06-22 23:17:33

How is using downto one less variable? I don't get that.

Also, note that using downto will not always be faster, as compilers may replace High(blah) with the value of High(blah), which would be faster. Another factor is that using downto can possibly decrease speed because of changes that may have to be made in the loop, array calling, and possibly influencing tail recursion.

(Note that my head is cluttered from watching too much of the Science channel, and that the speed is virtually the same regardless of using to or downto, especially in an interpreted language.)

Gu3st...

2011-06-23 04:19:39

From: i luffs yeww
How is using downto one less variable? I don't get that.

Also, note that using downto will not always be faster, as compilers may replace High(blah) with the value of High(blah), which would be faster. Another factor is that using downto can possibly decrease speed because of changes that may have to be made in the loop, array calling, and possibly influencing tail recursion.
You aren't making any sense…

You require one less variable because
var
I : Integer;
TIA : array [0..42] of Integer;

begin
for I := High(TIA) downto 0 do
WriteLn(TIA[I]);
end.

The bolded part doesn't make any sense, and the undelinedpart is why I said he should be careful.

i luffs yeww

2011-06-23 04:47:33


var
I : Integer;
TIA : array [0..42] of Integer;

begin
for I := 0 to High(TIA) do
WriteLn(TIA[I]);
end.

That is the same amount of variables?

Also, it's called inlining. Compilers do this to increase speed by reducing call overhead. Rather than for i := 0 to High(TIA) do, it would do for i := 0 to 42 do, which would be faster.

masterBB

2011-06-23 04:52:07

From: i luffs yeww
How is using downto one less variable? I don't get that.


It doesn't require one less variable, unless Pascal is doing some odd behind the scenes work with it. Just less calls to High(TIA).

Bixby Sayz

2011-06-23 05:19:56

From: JPHamlett
Was it


for i := 0 to high(Array) do
begin
code here
end;


That was what I was thinking about, which I already knew about. Just having a "senior moment" there for some reason. Coding when dead tired is such a bad idea.

Wizzup?

2011-06-23 05:41:13

From: i luffs yeww
Also, it's called inlining. Compilers do this to increase speed by reducing call overhead. Rather than for i := 0 to High(TIA) do, it would do for i := 0 to 42 do, which would be faster.


Yeah, but they don't, as least not PS.
Since high is dynamic, the array can change size in the loop. Try something like for i := 0 to high(a) do setlength(a, length(a)+1));

i luffs yeww

2011-06-23 05:45:47

I know. The inlining "fun fact" was more just in case someone just thinks that the way Zyt3x stated was the best isn't always.

nielsie95

2011-06-23 05:48:05

That's not inlining, but rather an optimization of the for-loop. If it was inlining, it would have the same behavior as in PS, and never end the loop if you keep increasing the size in the body of the loop. In Pascal Script, it should be faster to use an extra variable or use the downto loop.

Gu3st...

2011-06-24 05:33:07

From: i luffs yeww

var
I : Integer;
TIA : array [0..42] of Integer;

begin
for I := 0 to High(TIA) do
WriteLn(TIA[I]);
end.

That is the same amount of variables?

Also, it's called inlining. Compilers do this to increase speed by reducing call overhead. Rather than for i := 0 to High(TIA) do, it would do for i := 0 to 42 do, which would be faster.
That wouldn't be faster because of the fact that it's calling High(TIA) each time the loop starts over..
I don't think I would've posted in this thread if I didn't know what I was talking about, luffs

Rich

2011-06-24 06:36:05

Right. All this arguing calls for testing.
begin
for I:= 0 to High(II) do
WriteLn(II[I]);
WriteLn(IntToStr(GetTimeRunning));
end.
Took 406ms (average of 3 runs).

begin
for I:= High(II) downto 0 do
WriteLn(II[I]);
WriteLn(IntToStr(GetTimeRunning));
end.
Also took an average of 406ms (well, 405.6) (average of 3 runs).

Dgby714

2011-06-24 06:52:55

From: Rich
Right. All this arguing calls for testing.
begin
for I:= 0 to High(II) do
WriteLn(II[I]);
WriteLn(IntToStr(GetTimeRunning));
end.
Took 406ms (average of 3 runs).

begin
for I:= High(II) downto 0 do
WriteLn(II[I]);
WriteLn(IntToStr(GetTimeRunning));
end.
Also took an average of 406ms (well, 405.6) (average of 3 runs).


Here is a benchmark.
program _;

var
I: Integer;
T: LongWord;
II: TIntegerArray;

begin
SetLength(II, 50000);

T := GetTickCount;
for I := 0 to High(II) do
;
WriteLn(GetTickCount - T); //156

T := GetTickCount;
for I := High(II) downto 0 do
;
WriteLn(GetTickCount - T); //62
end.

Rich

2011-06-24 09:45:11

What's GetTickCount? I've never seen or heard of that before :o

Dgby714

2011-06-24 11:28:46

From: Rich
What's GetTickCount? I've never seen or heard of that before :o


Returns the amount of ms since the system started.
It's in Simba. I don't know if it's in SCAR but it can be using
function GetTickCount: LongWord; external 'GetTickCount@kernel32.dll stdcall';

Info Link: http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx

Rich

2011-06-24 12:42:00

Isn't that what GetSystemTime does?

Dgby714

2011-06-24 13:02:20

From: Rich
Isn't that what GetSystemTime does?


Probably… Your benchmark didn't really show the difference.

Wizzup?

2011-06-25 13:19:39

I'd suggest either GetSystemTime or GetTickCount (they are the same iirc), but not GetTimeRunning. You might be measuring other things as well with that one.