Performance tip : iterating through arrays

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Performance tip : iterating through arrays

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by Richard Mitchell >> Fri, 18 Dec 1998 2:39:43 GMT

For large arrays, it is exponentially faster to use an iterator (e.g. foreach instruction) to wheel through an array than it is to index your way through. Instead of coding:

while i <= array.size do
string := array;
i := i + 1;
endwhile

you should code

foreach str in array do
string := str;
endforeach;

Richard
-----------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Performance tip : iterating through arrays

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by Craig Shearer >> Tue, 5 Jan 1999 22:58:48 GMT

Can anybody shed some light on why this is so?

Also, why is it exponentially faster?

Craig.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Performance tip : iterating through arrays

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by Darrell Duniam >> Wed, 6 Jan 1999 1:38:03 GMT

I believe that the "size" method of the Collection class (inherited by the List class, which is in turn inherited by the Array class)
calculates the number of entries each time it's called. The same sort
of thing applies when reading thru list/combo boxes or table controls.

You could also avoid the problem by assigning the value returned by "size" to a variable, however, if you're adding or removing entries whilst in the loop, you'll probably find yourself up the proverbial creek.

darrell.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Performance tip : iterating through arrays

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by Richard Mitchell >> Sun, 10 Jan 1999 20:48:22 GMT

I used the word "exponentially" somewhat loosely. However it is my understanding that when you access an array element in JADE that JADE iterates through the array (starting at the beginning) to find the element that you requested. Obviously this means that as you get further into the array, it takes longer to locate the requested element.

Richard

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Performance tip : iterating through arrays

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by Craig Shearer >> Sun, 10 Jan 1999 23:18:37 GMT

If this is indeed correct then "exponentially" was used perfectly correctly :-)

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Performance tip : iterating through arrays

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by Eric Peachey >> Thu, 21 Jan 1999 23:03:44 GMT

Hello,

Beware of large StringArray and HugeStringArray objects. If you want to manipulate large arrays of strings then you'll find it heaps quicker to define your own string object class (has one attribute of String of maximum size)and a collection (say a Set or ExternalKeyDictionary) for storing your string objects. Access to your string objects in your collection will be vastly quicker than going through a StringArray or HugeStringArray.

Below is a defintion of a simple class and collection and a JadeScipt method for you to try out.

Have fun.

Eric Peachey


//****************************

STRING OBJECT CLASS DEFINTION...

//*********************************************
typeDefinitions
StringObject completeDefinition
(
attributeDefinitions
id: Integer;
myString: String;
)


//***************************************

MemberKeyDictionary for storing StringObjects

//****************************************************

typeHeaders
StringObjectDict subclassOf MemberKeyDictionary transient, number = 2131;

membershipDefinitions
StringObjectDict of StringObject ;

typeDefinitions
StringObjectDict completeDefinition
(
)


memberKeyDefinitions
StringObjectDict completeDefinition
(
id;
)


// **********************************

THE METHOD!!!

//*******************************************
testArrays();

vars
hsa : HugeStringArray;
so : StringObject;
sod : StringObjectDict;
s : String;
i : Integer;begin


write 'Creating objects...';

create hsa transient;
create sod transient;

write 'Populating collections...';
while i < 500 do
hsa.add( "Hello, I'm string number " & i.String );

create so transient;
so.id := i;
so.myString := "Hello, I'm string number " & i.String;
sod.add( so );
i := i + 1;
endwhile;

write 'Going through HugeStringArray...';
write app.actualTime();
i := 0;
foreach s in hsa do
i := i + s.length;
endforeach;
write 'Total length of strings = ' & i.String;
write app.actualTime();
write 'Done!';

write 'Going through Dictionary...';
write app.actualTime();
i := 0;
foreach so in sod do
i := i + so.myString.length;
endforeach;
write 'Total length of strings = ' & i.String;
write app.actualTime();
write 'Done!';

epilog

delete hsa;
delete sod;

end;


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 19 guests