Re: [flexcoders] speed of the for each looping
Sorry, couldn't resist commenting on the term cognitive cruft! I love it! :)) On Wed, Dec 10, 2008 at 12:37, Maciek Sakrejda [EMAIL PROTECTED] wrote: Interesting. I decided to actually try my test above, and on 100 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
[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 ?
Re: [flexcoders] speed of the for each looping
If you're trying to speed up your application, you should use the profiler. Something like the difference between a manually indexed for loop and a for-each loop is almost certain to be trivial. If you don't have the profiler, run an ad-hoc perf test by doing something like // we need to construct something to iterate over: var iterations:int = 100; var arr:Array = []; for (var i:int = 0; i iterations; i++) arr.push(i); var dummy:int = 0; var forBeginning:Number = new Date().getTime(); for (var j:int = 0; j iterations; j++) { // we need to do something in the loop so the whole thing // is not just optimized out (not sure if FP does this, but // it can), but we don't want to use trace() since I/O is // relatively expensive compared to the cost of the loop itself dummy = arr[j]; } trace(for loop took + (new Date().getTime() - forBeginning) + milliseconds); var forEachBeginning:Number = new Date().getTime(); for each (var k:int in arr) { dummy = k; } trace(for-each loop took + (new Date().getTime() - forEachBeginning) + milliseconds); That should give you an idea of the difference (but run this many times and average it, and vary the order of the loops). Also, keep in mind that most of the time, you won't be iterating over a million items. Bottlenecks can occur in surprising places (though they're often found in common places too, like Container layout code). Don't try to optimize things that *seem like* they would be slow without actually confirming not only that they *are* slow, but that they are affecting the overall speed or responsiveness of your application. It makes no sense to make a 10x performance improvement in one part of your code that accounts for .01% of your application's CPU usage. -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com -Original Message- From: Cato Paus [EMAIL PROTECTED] Reply-To: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com Subject: [flexcoders] speed of the for each looping Date: Tue, 09 Dec 2008 13:28:23 - 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 ?
RE: [flexcoders] speed of the for each looping
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: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Cato Paus Sent: Tuesday, December 09, 2008 5:28 AM To: flexcoders@yahoogroups.com 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 ?
RE: [flexcoders] speed of the for each looping
Interesting. I decided to actually try my test above, and on 100 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: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com flexcoders@yahoogroups.com 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:flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Cato Paus Sent: Tuesday, December 09, 2008 5:28 AM To: flexcoders@yahoogroups.com 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 ?
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]wrote: Interesting. I decided to actually try my test above, and on 100 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: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com flexcoders@yahoogroups.com 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:flexcoders@yahoogroups.com [EMAIL PROTECTED][mailto: [EMAIL PROTECTED] On Behalf Of Cato Paus Sent: Tuesday, December 09, 2008 5:28 AM To: flexcoders@yahoogroups.com 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 -- 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] :: http://flex.joshmcdonald.info/ :: http://twitter.com/sophistifunk
RE: [flexcoders] speed of the for each looping
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: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Josh McDonald Sent: Tuesday, December 09, 2008 3:51 PM To: flexcoders@yahoogroups.com 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 100 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: flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com To: flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com 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:flexcoders@yahoogroups.commailto:[EMAIL PROTECTED] [mailto:flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com] On Behalf Of Cato Paus Sent: Tuesday, December 09, 2008 5:28 AM To: flexcoders@yahoogroups.commailto:flexcoders@yahoogroups.com 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