Monday, October 11, 2010

ActionScript 3 Performance Analysis

I was reading the post written in a blog (post1, post2) where the author compares various languages (Erlang, Python, Ruby, C, JavaScript, Lua, PHP, Common Lisp, Haskell), using a program which computes Pythagorean triplets. I just tried with ActionScript 3 and I'm not able to post any comments in that blog. So I'm writing it here.
import flash.utils.Timer;

var t0:int = getTimer();
trace( "result: " + pythag( 5000 ) );
var t1:int = getTimer();
trace( "Elapsed Time (s): " + String( ( t1 - t0 ) / 1000 ) );

function pythag( n:int ):int
{
var a:Number;
var b:int;
var c:int;
var i:int;

for( c=2; c<=5000; c++ ) {
for( b=1; b < c; b++ ) {
a = Math.sqrt( c*c - b*b );
if( Math.floor( a ) == a ) {
i++;
}
}
}
return i;
}
Time elapsed is 3.53 s. That's fast. Giovanni Intini (the author of that post) was running on a dual core MacBook Pro. I'm running this on a Pentium D dual core. So it must be a fair comparison. So here are the comparison from that site:
C: 0.40s
Lua: 3.44s
ActionScript 3: 3.53s
Erlang(smp): 3.95s
Erlang (non-smp): 5.66s
PHP5: 8.9s
Python(2.5.1): 11.2s
Ruby: 12.30s
JavaScript: -- (script stopped -taking too much time).