25 Replies Latest reply on Aug 21, 2007 8:04 AM by lokalhorst

    AS3 performance

    lokalhorst
      Hi,

      i started with AS3 very recently ( i have AS2 experience...). I did some simple actionscript code to compare the performance
      between AS2 and AS3.

      Surprisingly, the AS3 version of the code produces a movieclip which consumes almost twice as much cpu as the AS2 version.
      I have no glue why that is.

      Please have a look at the code:

      1. AS2 version:

      a) Put the following in the maintimeline at frame 1:

      n = 300;
      xmin = 0;
      xmax = 550;
      ymin = 0;
      ymax = 400;

      for (i = 0; i < n; i++)
      {
      this.attachMovie("point","p" + i,i);
      o = this["p" + i];
      o._x = Math.floor(Math.random() * (xmax - xmin + 1)) + xmin;
      o._y = Math.floor(Math.random() * (ymax - ymin + 1)) + ymin;
      }


      b) create a library symbol with id "point", draw a small circle in it and add the following script to it:

      this.depth = Math.random();
      this._xscale = _yscale = depth * 100;
      this._alpha = depth * 100;
      var vx = depth;
      this.onEnterFrame = function()
      {
      this._x = this._x + vx;
      };

      2. AS3 version:

      a) Put the following in the maintimeline at frame 1:

      var n:Number = 300;
      var xmin:Number = 0;
      var xmax:Number = 550;
      var ymin:Number = 0;
      var ymax:Number = 400;

      for (var i = 0; i < n; i++)
      {
      var tempPoint = new point();
      tempPoint.x = Math.floor(Math.random() * (xmax - xmin + 1)) + xmin;
      tempPoint.y = Math.floor(Math.random() * (ymax - ymin + 1)) + ymin;
      this.addChild(tempPoint);
      }


      b) create a library symbol like above with Class "point" and add the following script to it:

      var depth:Number = Math.random();
      this.scaleX = this.scaleY = this.depth;// * 100;
      this.alpha = depth;
      var vx:Number = depth;

      this.addEventListener(Event.ENTER_FRAME, onEnterFrames);

      function onEnterFrames(event:Event)
      {
      this.x = this.x + vx;
      }

      Any Ideas?
        • 1. Re: AS3 performance
          Beatie3 Level 1
          My best answer is AS 3.0 is evil and was sent to thwart us.
          • 2. Re: AS3 performance
            lokalhorst Level 1
            Well, my best answer right know is, that i don't trust the statements anymore which acclaim that AS3 is not only more efficient than but also up to ten times 'faster' than AS2. One can read that in many blogs. But what is the truth??
            Are there any benchmarks ? I mean, at least AS3 shouldn't eat up more cpu than AS2 does, right ??
            • 3. Re: AS3 performance
              Beatie3 Level 1
              *Beatie grabs a big stick and beats AS 3.0 with it* Die AS 3.0! Die! Die! Die!

              Strange how much better that makes me feel.
              • 4. Re: AS3 performance
                kglad Adobe Community Professional & MVP
                the code you haven't posted for as2 and as3 are likely to be significant.

                if you're doing a simple test, post all the code.
                • 5. Re: AS3 performance
                  lokalhorst Level 1
                  kglad:

                  this IS ALL of the code (...of course, otherwise i wouldn't bother you with that detailed description how to use it) .
                  Please, place the code as described and do a simple test yourself !

                  • 6. Re: AS3 performance
                    Greg Dove Level 4
                    I'm interested in the 'truth' here also. I tried the two tests and didn't really notice much difference between them on my notebook in terms of CPU load (WinXP 1.7Ghz). Part of the reason for performance improvements in as3 is very likely due to code optimization.. e.g don't use a MovieClip for a static dot, use a sprite etc. But I had also assumed that similar code would also run faster. There's no cacheAsBitmap or anything being used here but that applies to both versions.

                    One thing to check is your actual fps performance as well. So what fps is actually being achieved in both. To do this it would probably make sense to have a single onEnterframe handler in the parent and have a loop inside it that processes the x for all the 'points' - instead of it being inside the points themselves - then with the the same code you can check the fps in both versions to see if what's being achieved there is any different.

                    And of course make sure both versions are running at the same frame rate which I'm sure you've already done.

                    I've only just started to dabble with as3 and was on the verge of porting something I'd done recently in as2 - mostly as a learning exercise - but I'm keen to hear what other's real experience is of the as3 'performance breakthroughs' - either in direct comparisons like this or with the optimisations that are possible in as3 code. I have to say that after having a play with XML in as3 I think it is far from evil - it made as2's XML support look evil.
                    • 7. Re: AS3 performance
                      cayennecode Level 1
                      well things I was doing in AS2 that sucked and were slow, are fast and work great in AS3 soooooooo........
                      • 8. Re: AS3 performance
                        kglad Adobe Community Professional & MVP
                        i didn't think your as3 code would compile with i being untyped. but it does.
                        • 9. Re: AS3 performance
                          lokalhorst Level 1
                          GWD,

                          the fps performance in AS3 with the posted example is worse than in AS2. Just try to increase the amount of points until your
                          display fps becomes sluggish: in the AS2 example i can go up to 4000 point, still having a display fps of 20 to 25.
                          In the AS3 example, the fps goes down to 5 to 10 with 4000 points ( on my machine)!! This relative comparison really makes me wonder how to achieve a performance increase with AS3 in that simple code.

                          cayennecode ,

                          if you have any example how you archieved performance increases with AS3 would be very helpfull. Maybe you can give some
                          advices how to optimize my AS3 example.
                          • 10. AS3 performance
                            Beatie3 Level 1
                            I'd be interested to know how many of the people who are finding it wonderful are actually using it in Flex 2 rather than Flash. The examples and tutorials I've found have all been geared to Flex 2 developers - which makes sense given how long it's been out - but very few for Flash designer/developers. I know as a deseloper my code is remedial by comparison to the proper coders, but that was always the joy of Flash when it belonged to Macromedia, it accommodated us very nicely. I now feel very much left by the wayside.
                            • 11. Re: AS3 performance
                              kglad Adobe Community Professional & MVP
                              i haven't had a chance to test your code, but i can tell you that you should see a significant performance benefit in as2 and as3 if you enable the cacheAsBitmap property of your movieclips and display objects.
                              • 12. Re: AS3 performance
                                Rothrock Level 5
                                I was doing some math in AS2 and then re-did it in AS3. I had worked hard in AS2 to get the code to execute in 90 seconds. With AS3, my first attempt got it down to about 10 seconds. So I am definitely seeing the speed benefit. Don't know about the memory requirements or framerate.
                                • 13. Re: AS3 performance
                                  kglad Adobe Community Professional & MVP
                                  i found no difference in cpu usage using the code below without enabling the cacheAsBitmap properties.

                                  when enabling cacheAsBitmap as3 realized about a 10% decrease in cpu usage while as2 dropped by about 20% and so as2 was more efficient running this code.

                                  as3 performed significantly better than as2 when using more reasonable code which i'll post in a message after this one.

                                  • 14. Re: AS3 performance
                                    kglad Adobe Community Professional & MVP
                                    when using the as2 and as3 code below, as3 outperformed as2 by a wide margin (22% cpu usage vs 30%):

                                    • 15. Re: AS3 performance
                                      kglad Adobe Community Professional & MVP
                                      p.s. the only difference between the poorly coded version and the "better" encoded version is the former has 3000 onEnterFrame loops and the latter has one. they both have 3000 movieclips moving across the stage.
                                      • 16. Re: AS3 performance
                                        Greg Dove Level 4
                                        @kglad ... I'm still very new to as3. I started to do something similar yesterday and had the as3 one done pretty much as you did it but didn't get as far as changing the as2 version for comparison. One other thing I did in the library properties was change the base class for 'point' from MovieClip to Sprite... after having moved the frame loop of its timeline like that. I didn't really notice any difference, but I wasn't very vigorous... but I am curious... should there be a least a theoretical improvement by doing that as well? Or does cacheAsBitmap kind of eliminate that as a possible as3 advantage for a static symbol like this?
                                        • 17. Re: AS3 performance
                                          lokalhorst Level 1
                                          kglad,
                                          the only performance wise difference between the "poorly" coded version and the superior encoded version is the use of cacheAsBitmap, (as i would expect from the theoretical point of view).-

                                          cacheAsBitmap has another side of the medal: all bitmap rendered vectors reside in your physical memory. Depending on how
                                          complex your vector animation is, i consider cacheAsBitmap as a "trick" to virtually increase the performance.

                                          Without such tricks, i can't see how i can profit from AS3 considering such simple animation stuff ( though i would use AS3 for other projects). For more sophisticated animation, for instance PaperVision3D etc. AS3 is the only way.

                                          • 18. Re: AS3 performance
                                            Greg Dove Level 4
                                            @lokalhorst:

                                            I'm sorry but I don't understand your point. cacheAsBitmap is a "trick" to increase performance. That is true. But its the same trick in as2 or as3... for the same reasons. Its not as3 specific.
                                            The key difference between the old code and the 'enhanced' code was actually the use of a single onEnterFrame loop with an internal for loop instead of multiple onEnterFrames in each of the 'point' clips, which is - I believe - pretty standard for optimising code. I would be inclined to favour this approach in as2 for the same reason, so I wouldn't consider it as an as3 only approach.
                                            • 19. Re: AS3 performance
                                              lokalhorst Level 1
                                              GWD,

                                              "The key difference between the old code and the 'enhanced' code was actually the use of a single onEnterFrame loop "

                                              ?? The key difference in Performance IS NOT where the onEnterFrame is placed !

                                              Using the same (at least concerning the examples above...) code in AS2 and AS3 gives you almost the same performance.
                                              If the code is 'optimized' (concerning the placement of the enterFrame ) or not, in each case the relative comparison gives you almost the same bad/good performance in AS2 and AS3.

                                              That is what kglad said: "i found no difference in cpu usage using the code below without enabling the cacheAsBitmap properties"


                                              Obviously cacheAsBitmap makes the difference. With the use of cacheAsBitmap, unspecific if AS2 or 3 , both AS2 and AS3 runs faster ( the bad code as the good code aswell) whereas the AS3 version runs 5% faster ( does it ?).
                                              So the relative comparison between AS2 and AS3 using such simple code hardly doesn't show any performance difference between AS2 and 3. That's what i said.






                                              • 20. Re: AS3 performance
                                                Greg Dove Level 4
                                                Please take a look at kglad's comments again.

                                                Both examples of the code had cacheAsBitmap switched on - but without it he said there was no difference (which I observed also - but which is NOT what you observed), with cacheAsBitmpa he said that as2 was actually slightly better.

                                                The second case was with the centralised enterFrame loop (again with cacheAsBitmap switched on for both). Here there was a big difference in favour of as3.

                                                So no - it wasn't cacheAsBitmap that made the difference.


                                                • 21. Re: AS3 performance
                                                  lokalhorst Level 1
                                                  Ok. to stress this on more time: here is what i meassured.

                                                  n = 8000 points.

                                                  AS2, not optimized code:

                                                  1) cacheAsBitmap Off: FPS 11
                                                  2) cacheAsBitmap On: FPS 18

                                                  AS2, optimized code:

                                                  1) cacheAsBitmap Off: FPS 11
                                                  2) cacheAsBitmap On: FPS 18

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

                                                  AS3, not optimized code:

                                                  1) cacheAsBitmap Off: FPS 3 (!!)
                                                  2) cacheAsBitmap On: FPS 8

                                                  AS3, optimized code:

                                                  1) cacheAsBitmap Off: FPS 11
                                                  2) cacheAsBitmap On: FPS 27

                                                  So, AS3 can be much faster than AS2 ONLY when cacheAsBitmap is applied. Even with optimzed code it's only quite as fast as AS2.
                                                  That is surprising, at least to me !!

                                                  • 22. Re: AS3 performance
                                                    kglad Adobe Community Professional & MVP
                                                    no, gwd has it exactly correct: i only did one test without enabling cacheAsBitmap and i did that with the poorly coded version that contains 3000 onEnterFrame loops.

                                                    the big difference in the poorly coded and better coded versions is the use of one onEnterFrame loop in the better coded version and that only testing i did there was with cacheAsBitmap enabled. i don't think it's going to make much difference disabling it because it didn't make much difference in the poorly coded version.

                                                    from all this i conclude:

                                                    that as3 is NOT significantly less cpu instensive than as2 in ALL situations. in fact, there are some situations where it is somewhat more cpu intensive.

                                                    on the other hand, i haven't seen any situation where as3 is significantly more cpu intensive than as2 and there are situations where as3 is significantly less cpu intensive than as2.

                                                    and, more importantly, cpu usage is not the same as speed of code execution which is the area where as3 is supposed to outstrip as2.

                                                    • 23. AS3 performance
                                                      Greg Dove Level 4
                                                      OK... now I understand your point. My experience was not the same . Neither - according to his posts was kglad's. But then the numbers of points were lower. So I guess there are a lot of other variables that are not factored in like system and (possibly) player differences across OSes.

                                                      I'll run the tests later on as well with fps calculations, but I can say already that I did not observe that fps difference visually - it should be observable - but then I had my doc property set at 25fps for both anyhow - and perhaps you had it higher. I do agree that it is important to look at performance like that as well in terms of an output measure (fps, calculations per second, xml nodes parsed per sec or whatever is relevant) and not just resource consumption. Its the balance that's important and the there are of course times when one is more important than the other.

                                                      I would love to see some independent benchmarks done. I feel a bit dubious about the results even with switching off IM, gmail notifier, and all the other system tray stuff to try to establish something not too distant from a control setting.


                                                      • 24. Re: AS3 performance
                                                        Greg Dove Level 4
                                                        Whenever I say "I wish there were" or "I'd love to see" I often follow up with a google to make sure there weren't in fact the things that I wanted already in existence. (I know that's the wrong way around... but.anyways)
                                                        So I couldn't find any benchmarks. But perhaps this is worth a quick perusal - it kind of mirrors your most recent comment, kglad.

                                                        http://www.actionscript.org/forums/showthread.php3?t=112454

                                                        Unless there is a huge conspiracy to cover it up - I can assure you I am not part of it ;-) - then I couldn't find anything that suggested slower rendering performance for as3 code. What that thread suggests is that rendering is not faster - and it answered my earlier question about the theoretical use of Sprite instead of MovieClip.
                                                        • 25. Re: AS3 performance
                                                          lokalhorst Level 1
                                                          kglad you wrote:
                                                          "i only did one test without enabling cacheAsBitmap and i did that with the poorly coded version that contains 3000 onEnterFrame loops".
                                                          Your conclusion was:
                                                          " i found no difference in cpu usage using the code below without enabling the cacheAsBitmap properties"

                                                          I found a difference i my test, AS3 was much slower here, but at least you agree, AS3 is not better.

                                                          kglad you did a second test:
                                                          "the big difference in the poorly coded and better coded versions is the use of one onEnterFrame loop in the better coded version and that only testing i did there was with cacheAsBitmap enabled"

                                                          i found the same result in my test, didn't i ? AS3 with almost 30% performance increase to AS2 here.

                                                          I made a third test, disabling cacheAsBitmap and comparing the optimized AS2 and AS3. The result was: AS2 and AS3 nearly the same.

                                                          Instead of doing that test yourself or just read my mail correctly you state:

                                                          "i don't think it's going to make much difference disabling it because it didn't make much difference in the poorly coded version."


                                                          It really has no subtsance talking about what you believe how the things could be or not. Just read the numbers.
                                                          Therefore your "conclusion" you gave us is "poorly" deduced, isn't it. The conclusion of what you believe or what??

                                                          "i haven't seen any situation where as3 is significantly more cpu intensive than as2 "

                                                          Well, i have so far.

                                                          "and, more importantly, cpu usage is not the same as speed of code execution which is the area where as3 is supposed to outstrip as2"
                                                          Of course that is trivial. And we all know that AS3 is "supposed" to do a lot of things.

                                                          That's why i started that thread. Just to find out learn by experience.