Comments on: AS3 Performance Optimization http://www.richnetapps.com/as3-performance-optimization/ Internet Applications - Flash, Flex, Silverlight, JavaFX Sat, 03 Jul 2010 12:30:55 +0000 hourly 1 http://wordpress.org/?v=3.0 By: Armand Niculescuhttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-578 Armand Niculescu Thu, 22 Oct 2009 19:18:32 +0000 #comment-578 Dave, in your specific example, yes, the first method is faster. This shows that for simple cases, the compiler is clever enough to optimize the code by itself. However, instead of using that simple array, generate one, like this: for (i=0; i<50000; i++) <code>{ aa = new Array(); for (j=0; j<100; j++) aa[j]="a"+j; a[i]=aa; }</code> The second method becomes significantly faster (in my case 41ms vs 25ms for 50000 iterations).I have some notes from Werner Sharp, an Adobe engineer on this matter, who helped me improve the performance of my <a href="http://www.flashchess3.com" rel="nofollow">chess game</a>. The increase in speed was dramatic.To quote Mr. Sharp (a little out of context): " <em>The player does not know the type of the array so it goes through a much slower method. It’s not the objects in the array but the array reference itself. [...] For arrays whose element are untyped, just make sure you type them when you retrieve the value. <code>var x:MyType = myarray[i].property</code> becomes <code>var t:MyObject = myarray[i]; var x:MyType = t.property;</code>In FP10, you could use the Vector object for type safety and performance.</em>"I couldn't use Vector at the time.Also have a look at http://www.adobe.com/devnet/flex/articles/as3_tuning/fm_as3perf.pdf Dave, in your specific example, yes, the first method is faster. This shows that for simple cases, the compiler is clever enough to optimize the code by itself.
However, instead of using that simple array, generate one, like this:
for (i=0; i<50000; i++)
{
aa = new Array();
for (j=0; j<100; j++)
aa[j]="a"+j;
a[i]=aa;
}

The second method becomes significantly faster (in my case 41ms vs 25ms for 50000 iterations).

I have some notes from Werner Sharp, an Adobe engineer on this matter, who helped me improve the performance of my chess game. The increase in speed was dramatic.

To quote Mr. Sharp (a little out of context): ” The player does not know the type of the array so it goes through a much slower method. It’s not the objects in the array but the array reference itself. [...] For arrays whose element are untyped, just make sure you type them when you retrieve the value.
var x:MyType = myarray[i].property
becomes
var t:MyObject = myarray[i];
var x:MyType = t.property;

In FP10, you could use the Vector object for type safety and performance.

I couldn’t use Vector at the time.

Also have a look at http://www.adobe.com/devnet/flex/articles/as3_tuning/fm_as3perf.pdf

]]>
By: Davehttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-577 Dave Thu, 22 Oct 2009 18:38:48 +0000 #comment-577 Thanks for the tips! The one about keeping strongly typed with arrays seems wrong however - I did various tests of this one and I always am getting better performance when I do like: b = myArray[n][i]; instead of the two line method you showed. Here's a super simple test:var b:int; var a:Array = new Array([50,50,50],[20,20,20],[30,30,30],[10,10,10]); var aa:Array = new Array();d = getTimer(); for(i = 0; i < 5000000; i++){ b = a[2][2]; } e = getTimer() - d;d = getTimer(); for(i = 0; i < 5000000; i++){ aa = a[2]; b = aa[2]; } f = getTimer() - d; trace(e,f);e is always coming out faster? Thanks for the tips! The one about keeping strongly typed with arrays seems wrong however – I did various tests of this one and I always am getting better performance when I do like: b = myArray[n][i]; instead of the two line method you showed. Here’s a super simple test:

var b:int;
var a:Array = new Array([50,50,50],[20,20,20],[30,30,30],[10,10,10]);
var aa:Array = new Array();

d = getTimer();
for(i = 0; i < 5000000; i++){
b = a[2][2];
}
e = getTimer() – d;

d = getTimer();
for(i = 0; i < 5000000; i++){
aa = a[2];
b = aa[2];
}
f = getTimer() – d;
trace(e,f);

e is always coming out faster?

]]>
By: Nickhttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-538 Nick Tue, 26 May 2009 20:22:02 +0000 #comment-538 Great article. I was wondering if you had any insight into using the array utilities such as array.getIndex() if they are faster then looping through the array and comparing the values? Or maybe you have some tips about combining arrays without entering duplicate values?Thanks in advance!<strong>Armand:</strong> <code>.getIndex()</code> is indeed faster than looping. I don't have benchmark info nearby but it's difinitely noticeable. However, other methods such as <code>Array.map()</code> or <code>.every()</code> or <code>.some()</code> are rather slow. Great article. I was wondering if you had any insight into using the array utilities such as array.getIndex() if they are faster then looping through the array and comparing the values? Or maybe you have some tips about combining arrays without entering duplicate values?

Thanks in advance!

Armand: .getIndex() is indeed faster than looping. I don’t have benchmark info nearby but it’s difinitely noticeable. However, other methods such as Array.map() or .every() or .some() are rather slow.

]]>
By: Blogger Indramayuhttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-537 Blogger Indramayu Sat, 23 May 2009 10:45:10 +0000 #comment-537 Thanks for share. I need this. Thanks for share. I need this.

]]>
By: iPhonehttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-171 iPhone Tue, 24 Feb 2009 02:17:53 +0000 #comment-171 Thanks for the great post..It covers a lots of topics and got to know so much about coding through this..Keep up the good work and i will be looking for some more such posts on your blog. Thanks for the great post..It covers a lots of topics and got to know so much about coding through this..Keep up the good work and i will be looking for some more such posts on your blog.

]]>
By: NoelBhttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-170 NoelB Tue, 09 Dec 2008 11:46:49 +0000 #comment-170 I haven't spent much time with flash 10, but it's amazing to hear that simply typing the elements of an array will grant you similar performance as using vectors in flash 10. Great collection of tips, thanks for the post. I haven’t spent much time with flash 10, but it’s amazing to hear that simply typing the elements of an array will grant you similar performance as using vectors in flash 10. Great collection of tips, thanks for the post.

]]>
By: Erikhttp://www.richnetapps.com/as3-performance-optimization/comment-page-1/#comment-169 Erik Fri, 28 Nov 2008 04:20:38 +0000 #comment-169 Very nice article. I really like that you are putting emphasis on the 'don’t optimize early' and the 'do you need to optimize' parts.<br /> <br /> It would be nice if we had an ActionScript pre compiler who would make these kinds of optimalizations before actualy compiling code. Who knows, someone might feel up to it, hehe.<br /> <br /> <br /> Greetz Erik Very nice article. I really like that you are putting emphasis on the ‘don’t optimize early’ and the ‘do you need to optimize’ parts.

It would be nice if we had an ActionScript pre compiler who would make these kinds of optimalizations before actualy compiling code. Who knows, someone might feel up to it, hehe.

Greetz Erik

]]>