Groups | Blog | Home
all groups > flash actionscript > august 2007 >

flash actionscript : AS3 performance


lokalhorst
8/19/2007 9:19:38 PM
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?

lokalhorst
8/20/2007 12:00:00 AM
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 ??
Beatie3
8/20/2007 12:00:00 AM
*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.
kglad
8/20/2007 12:00:00 AM
the code you haven't posted for as2 and as3 are likely to be significant.

Beatie3
8/20/2007 5:31:29 AM
My best answer is AS 3.0 is evil and was sent to thwart us.
lokalhorst
8/20/2007 3:49:09 PM
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 !

GWD
8/20/2007 5:01:11 PM
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.
cayennecode
8/20/2007 5:24:48 PM
kglad
8/20/2007 5:57:02 PM
lokalhorst
8/20/2007 5:58:39 PM
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. :smile;
Beatie3
8/20/2007 10:15:23 PM
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 accomodated us
very nicely. I now feel very much left by the wayside.
:sad;
kglad
8/20/2007 10:26:22 PM
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.
lokalhorst
8/21/2007 12:00:00 AM
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.


GWD
8/21/2007 12:00:00 AM
@lokalhorst:

I'm sorry but I don't understand your point. cacheAsBitmap [b]is[/b] 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.

lokalhorst
8/21/2007 12:00:00 AM
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.







GWD
8/21/2007 12:00:00 AM
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.



Rothrock
8/21/2007 1:54:03 AM
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.

kglad
8/21/2007 4:13:16 AM
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.



// as2:

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

for (i=0; i<n; i++) {
o = this.attachMovie("point", "p"+i, i);
depth = Math.random();
with (o) {
cacheAsBitmap=true;
_x = Math.floor(Math.random()*(xmax-xmin+1))+xmin;
_y = Math.floor(Math.random()*(ymax-ymin+1))+ymin;
_xscale = depth*100;
_yscale = depth*100;
_alpha = depth*100;
}
o.vx = depth;
o.onEnterFrame = function() {
this._x += this.vx;
};
}

// as3:

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

for (var i:int = 0; i < n; i++) {
var tempPoint = new point();
var depth:Number = Math.random();
with (tempPoint) {
cacheAsBitmap = true;
x = Math.floor(Math.random() * (xmax - xmin + 1)) + xmin;
y = Math.floor(Math.random() * (ymax - ymin + 1)) + ymin;
scaleX = depth;// * 100;
scaleY =depth;
alpha = depth;
addEventListener(Event.ENTER_FRAME, onEnterFrames);
}
tempPoint.vx=depth
this.addChild(tempPoint);
}

function onEnterFrames(event:Event) {
event.currentTarget.x+= event.currentTarget.vx;
}
kglad
8/21/2007 4:15:04 AM
when using the as2 and as3 code below, as3 outperformed as2 by a wide margin
(22% cpu usage vs 30%):



// as2

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

mcA = [];

for (i=0; i<n; i++) {
o = this.attachMovie("point", "p"+i, i);
depth = Math.random();
with (o) {
cacheAsBitmap = true;
_x = Math.floor(Math.random()*(xmax-xmin+1))+xmin;
_y = Math.floor(Math.random()*(ymax-ymin+1))+ymin;
_xscale = depth*100;
_yscale = depth*100;
_alpha = depth*100;
}
o.vx = depth;
mcA.push(o);
}
this.onEnterFrame = function() {
for (var i = 0; i<mcA.length; i++) {
mcA[i]._x += mcA[i].vx;
}
};

// as3

var n:Number = 3000;
var xmin:Number = 0;
var xmax:Number = 550;
var ymin:Number = 0;
var ymax:Number = 400;
var mcA:Array=[];

for (var i:int = 0; i < n; i++) {
var tempPoint = new point();
var depth:Number = Math.random();
with (tempPoint) {
cacheAsBitmap = true;
x = Math.floor(Math.random() * (xmax - xmin + 1)) + xmin;
y = Math.floor(Math.random() * (ymax - ymin + 1)) + ymin;
scaleX = depth;// * 100;
scaleY =depth;
alpha = depth;

}
mcA.push(tempPoint);
tempPoint.vx=depth;
this.addChild(tempPoint);
}
addEventListener(Event.ENTER_FRAME, onEnterFrames);
function onEnterFrames(event:Event) {
for (var i:int=0; i<mcA.length; i++) {
mcA[i].x += mcA[i].vx;
}
}
kglad
8/21/2007 4:16:47 AM
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.
GWD
8/21/2007 6:11:12 AM
@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?
lokalhorst
8/21/2007 1:17:14 PM
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 !!


kglad
8/21/2007 1:49:11 PM
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.


GWD
8/21/2007 1:51:54 PM
OK... now I understand your point. My experience was not the same . Neither -
according to his posts was kgad'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.



GWD
8/21/2007 2:34:52 PM
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.

lokalhorst
8/21/2007 3:04:54 PM
[quoted text, click to view]
"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.















AddThis Social Bookmark Button