10 Replies Latest reply on Jul 5, 2007 11:17 AM by adam@blueapplestudio

    Code Optimization: Animation Slow Down

    adam@blueapplestudio Level 1
      There is a large block of code that causes my animation (which occurs over 15 frames) to slow down when it executes. Currently the code exists on one frame within the 15 frame span. The code has a bunch of functions, nested for loops, etc. I can’t post the code, so I am wondering if there are some general things to look at when this problem is encountered.

      My initial thought is to unwrap a bunch of the loops and distribute the code across different frames. 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. (The last two points are from http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Par t_ASLR.html)

      Are these good places to start? Any other hints in repairing the turtle I’ve built?
        • 1. Re: Code Optimization: Animation Slow Down
          kglad Adobe Community Professional & MVP
          i don't believe either of your statements are true and i don't find anything about those statements on the page you referenced.
          • 2. Re: Code Optimization: Animation Slow Down
            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.
            • 3. Re: Code Optimization: Animation Slow Down
              adam@blueapplestudio Level 1
              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.
              • 4. Re: Code Optimization: Animation Slow Down
                Level 7
                >>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

                Dave -
                Head Developer
                Adobe Community Expert

                • 5. Re: Code Optimization: Animation Slow Down
                  SymTsb Level 2
                  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.
                  • 6. Re: Code Optimization: Animation Slow Down
                    adam@blueapplestudio Level 1
                    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.
                    • 7. Re: Code Optimization: Animation Slow Down
                      kglad Adobe Community Professional & MVP
                      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.
                      • 8. Re: Code Optimization: Animation Slow Down
                        SymTsb Level 2
                        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.
                        • 9. Re: Code Optimization: Animation Slow Down
                          kglad Adobe Community Professional & MVP
                          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.
                          • 10. Re: Code Optimization: Animation Slow Down
                            adam@blueapplestudio Level 1
                            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.