Log in

View Full Version : Am I misremembering?



Bixby Sayz
06-23-2011, 12:27 AM
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
06-23-2011, 12:35 AM
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
06-23-2011, 12:44 AM
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
06-23-2011, 12:55 AM
Was it



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

Zyt3x
06-23-2011, 12:55 AM
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
06-23-2011, 03:17 AM
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.)

Zyt3x
06-23-2011, 08:19 AM
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
06-23-2011, 08:47 AM
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
06-23-2011, 08:52 AM
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
06-23-2011, 09:19 AM
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?
06-23-2011, 09:41 AM
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
06-23-2011, 09:45 AM
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
06-23-2011, 09:48 AM
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.

Zyt3x
06-24-2011, 09:33 AM
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
06-24-2011, 10:36 AM
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
06-24-2011, 10:52 AM
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
06-24-2011, 01:45 PM
What's GetTickCount? I've never seen or heard of that before :o

Dgby714
06-24-2011, 03:28 PM
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
06-24-2011, 04:42 PM
Isn't that what GetSystemTime does?

Dgby714
06-24-2011, 05:02 PM
Isn't that what GetSystemTime does?

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

Wizzup?
06-25-2011, 05:19 PM
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.