Interesting. I decided to actually try my test above, and on 1000000
items, the for-each version takes ~50 milliseconds, versus ~25
milliseconds for the explicitly indexed loop. When doing some actual
work in the loop (a trace), the numbers are 41.9 seconds for the
for-each and 41.1 seconds for the indexed for. On a loop with a trace
with 100 items, both forms take ~5 milliseconds. This is rather
unscientific, but I don't have the profiler available (will it ever make
it to Linux, Adobe?).

So yes, it looks like for-each is a lot slower in some cases, but I'll
maintain it still probably won't make a difference unless you've got a
massive loop that does very little, or a deeply nested set of loops.

Consider also the readability and maintainability benefits of a
for-each: unless you need the index, it's just one more place to
introduce bugs when refactoring, and it's cognitive cruft when trying to
follow what's going on.
-- 
Maciek Sakrejda
Truviso, Inc.
http://www.truviso.com

-----Original Message-----
From: Alex Harui <[EMAIL PROTECTED]>
Reply-To: [email protected]
To: [email protected] <[email protected]>
Subject: RE: [flexcoders] speed of the "for each" looping
Date: Tue, 9 Dec 2008 12:30:19 -0800

For each should be much slower than a basic iterator as it has to walk
the object’s properties.

 

For each (var p:* in someObject)

 

And

 

For (var p:String in SomeObject)

 

Probably run at the same rate.

 

From:[email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Cato Paus
Sent: Tuesday, December 09, 2008 5:28 AM
To: [email protected]
Subject: [flexcoders] speed of the "for each" looping


 

Hi, all you experts :)

I'm tying to speed up my application and I use a lot of

exsample
for (var i:int = 0; i < 5; i++)
{
trace(i);
} 

so is the "for each" looping faster ?


 


Reply via email to