They are different animals.  Scenarios where you could choose between 
index-based or property-based iteration don't come to mind.

Also note property-based iteration can perform much more slowly iterating 
proxy-based objects, and do not work on sealed non-dynamic classes.

-Alex

From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Josh 
McDonald
Sent: Tuesday, December 09, 2008 3:51 PM
To: [email protected]
Subject: Re: [flexcoders] speed of the "for each" looping


*nods*

I find that it's often much easier to read when you use for..in and for 
each..in rather than regular for. And since you need to have a "var current = 
list[i]" or similar as the first line, If you only need an index to display, or 
it's 1-based as opposed to 0-based, using a "for [each]..in" and having the 
first inner line be "++idx" will be easier to read than a bunch of statements 
within your loop that look like:

var current = foo[i+1]

or

msg = "you're at item #" + (i + 1)

If you want to know more about for each and how it works (outside of Arrays at 
least), look into flash.Proxy. The documentation is a little unclear at times 
on Proxy, so I wrote a couple of posts on using it here:

http://flex.joshmcdonald.info/2008/09/using-proxy-class-part-1_04.html
http://flex.joshmcdonald.info/2008/09/using-proxy-class-part-2.html

* IMHO, YMMV, (code) beauty is in the eye of the beholder, etc... =]

-Josh
On Wed, Dec 10, 2008 at 9:37 AM, Maciek Sakrejda <[EMAIL 
PROTECTED]<mailto:[EMAIL PROTECTED]>> wrote:
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]<mailto:[EMAIL PROTECTED]>>
Reply-To: [email protected]<mailto:[email protected]>
To: [email protected]<mailto:[email protected]> 
<[email protected]<mailto:[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]> 
[mailto:[email protected]<mailto:[email protected]>] On
Behalf Of Cato Paus
Sent: Tuesday, December 09, 2008 5:28 AM
To: [email protected]<mailto:[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 ?






------------------------------------

--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Alternative FAQ location: 
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! 
Groups Links

   Individual Email | Traditional





--
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

Like the cut of my jib? Check out my Flex blog!

:: Josh 'G-Funk' McDonald
:: 0437 221 380 :: [EMAIL PROTECTED]<mailto:[EMAIL PROTECTED]>
:: http://flex.joshmcdonald.info/
:: http://twitter.com/sophistifunk

Reply via email to