This content has been marked as final. Show 10 replies
i don't believe either of your statements are true and i don't find anything about those statements on the page you referenced.
Can you modify the code enough to be unrecognizable or unusable so that we could at least see it. It'd be hard to suggest modifications for optimizations without having anything to go on. Nested for loops could certainly slow down the process but are often needed. If you're recalling the same function over and over again so that it's in an infinite loop...you'll definitely slow down to a crawl. I don't think splitting it across multiple frames will help very much. I generally code all on one frame or separate .as files.
Sorry about the link. It does not appear to take you to the location within the AS Language Reference that I am citing. Go to the Language Reference ( http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Par t_ASLR.html), then click on Using Action Script>Using Best Practices>Performance And Flash Player>Optimizing Action Script in Flash Player. The first and sixth bullet points at the top of the page are what I tried to summarize.
Thanks for your willingness to help but I can’t post code, very strict policy. And I promise that there are no infinite loops; everything terminates. I am really looking for general pointers regarding the cleaning up of loops or code optimization… something like that.
>>I've also recently read that one should not use an array's length as the
>>condition in a for loop, and that calling a function over and over again
>>within a loop is discouraged.
In general, yes those are things to look at when optimizing your code. The
first one for sure, it's always better to get the array length one time and
stick it in a variable rather than getting it over and over. The second
depends... if you can eliminate calling something many times, and just do it
once, like the array length, it will be faster. But sometimes you can't help
Adobe Community Expert
IF you have a large number of for loops and you are using an array length to determine the iterations, assign the length to a variable and test the variable. It takes less processor power and less of the Flash engine to test a variable. If you are using the Array.length property, Flash has to look at the array (which could be huge) and then determine the length which means running an additional mathematical computation on each iteration of the loop. For most small loops this is fine but if you are using larger arrays, it could get very very ugly.
Are you doing anything it BitmapData or geometric functions in fast passes to draw on the screen? Anything like this will have an impact.
Are you importing classes on this frame? Are you only importing the classes your project needs or are you doing a "catch-all" with the * symbol?
Are you experiencing the problems from every computer you look at it on? or just yours? Are you having problems when running the IDE or from the browser?
I'd look at this stuff and see what you find. If you need more help, drop another line.
Thanks for the responses.
Regarding array length, I assumed that if array length was used as the loop's condition that it was calculated once. However, since array length is calculated at the top of each loop iteration (thanks SymTsb) then I can see how creating a variable for the array length at the front end would save resources. Does it do this because there may be something in the loop that could change the original array's length, and thereby changing the loop's condition?
The advice on functions is not real intuitive to me, but I will look at that as well.
Not using geometric functions, imported classes, etc. There are just a heap of for loops, switches, and the like that are chomping through a lot of data.
Thanks for your assistance, everyone.
it takes about 1 second longer for flash to determine an array's length 10,000,000 times compared with reading a variable's value and it takes about 1 second longer to a call a function 10,000,000 times compared with including the functions code in-line.
Although flash itself is capable of doing such calculations, the processors on the computer may not.
Anytime you are handling arrays, you will save on processor power across multiple platform/system setups by calling the arrays length from variable rather than having the processor (not just Flash) run an unnecessary mathematical calculation. This is true in every programming language I've ever used where arrays or sets or lists were possible.
Just because Flash can do something i.e. loop through 10,000,000 images in 10 minutes doesn't mean that the graphics processor and math cpus can. If he is crunching through a lot of mathematical computations, and wants to do so more quickly, as far as Flash is concerned, using a variable could take 2 or 3 seconds off the processing time.
With that said, also look at areas where you can streamline the code. Make sure you logic can't be approached by some more easily processed equation or such. I've seen too many people do complex math computations in a program where simplification or geometric/arithmetic summations would have been better suited.
Are you relying on an external scripting language to get data into Flash such as php or asp? Does the scripting language process data from a large database on the same frame as you are running your calculation etc.? Is this structured data and if so, what format is it being imported into Flash (XML or text)? These are other areas to consider for possible cause of delay as well. Just thought I'd throw them out there.
my point is i'll bet it isn't a significant cause of delay to use function calls and calculate array.length in each loop iteration.
it takes 10,000,000 loops to realize an appreciable time savings on my 2ghz amd cpu. it's close to inconceivable that any loop that large would execute so quickly that shaving 1 or 2 seconds would make an appreciable difference.
the big issue is what's going on in those loops and especially what's happening on-stage that the gpu must handle.
In four different four-loops I replaced the old condition (myArray.length) with a new variable (nArrayLength) created before the loop. This has delivered a noticeable performance improvement. Looking back at what's going on within the loops and different functions, there is some house cleaning to be done. That's my next step.
It is also worth noting that there is a lot happening on the stage during these calculations. A movie is playing, as well as some key framed animation. The gpu is likely getting a good workout, as kglad suggested.
Thanks again for all of your excellent help.