ActionScript3 speed test

Just read this article about programming language speeds:

Performance Comparison – C++ / Java / Python / Ruby/ Jython / JRuby / Groovy
by Dhananjay Nene

And of course, my first thought – let’s test ActionScript3 in this (a bit arguable) manner

The results were quite surprising, it beats PHP by far:

AS3 (Activex player 9,0,124,0, nondebug): 50 microseconds
AS3 (Stand-alone player, 9.0.115.0, debug): 150 microseconds
PHP (apache module): 600 microseconds
(on the same machine – Core2 Duo 2.4GHz, php running into vmware/debian. If I run the php version in windows, it gets worse :D )

Here is the AS version of the same program.
I compiled it with Flex Builder3, release mode (not debug, that’s a bit slower).

package 
{
	import flash.display.Sprite;
	import flash.text.TextField;
 
	public class speed_test extends Sprite
	{
		public function speed_test()
		{
			var ITER:int = 10000;
			var start:Number = (new Date).getTime(); 
			for (var i:int = 0 ; i < ITER ; i++)
			{
				var chain:Chain = new Chain(40);
				chain.kill(3);
			}
			var end:Number = (new Date).getTime();
 
			var txt:TextField = new TextField;
			txt.width = 300;
			addChild(txt);
			txt.text = "Time per iteration = " + 
				((end - start) / ITER * 1000) + " us.";
		}
	}
}
 
 
class Chain
{
	private var first:Person = null;
 
 
	public function Chain(size:int)
	{
		var last:Person = null;
		var current:Person = null;
		for (var i:int = 0 ; i < size ; i++)
		{
			current = new Person(i);
			if (!first) first = current; 
			if (last)
			{
				last.setNext(current);
				current.setPrev(last);
			}
			last = current;
		}
		first.setPrev(last);
		last.setNext(first);		
	}
 
	public function kill(nth:int):Person
	{
		var current:Person = first;
		var shout:int = 1;
		while(current.getNext() != current)
		{
			shout = current.shout(shout, nth);
			current = current.getNext();
		}
		first = current;
		return current;
	}
 
	public function getFirst():Person
	{
		return first;
	}	
}
 
class Person
{
	private var count:int;
	private var prev:Person = null;
	private var next:Person = null;
 
	public function Person(count:int)
	{
		this.count = count;
	}
 
	public function shout(shout:int, deadif:int):int
	{
		if (shout < deadif) return (shout + 1);
		this.getPrev().setNext(this.getNext());
		this.getNext().setPrev(this.getPrev());
		return 1;
	}
 
	public function getCount():int
	{
		return this.count;
	}
 
	public function getPrev():Person
	{
		return prev;
	}
 
	public function setPrev(prev:Person):void
	{
		this.prev = prev;
	}
 
	public function getNext():Person
	{
		return next;
	}
 
	public function setNext(next:Person):void
	{
		this.next = next;
	}
}

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

AddThis Social Bookmark Button

4 Responses to “ActionScript3 speed test”

  1. Very cool! When deciding whether I should process some data at the server (PHP) or the client (AS3) I’ve considered datasize, and server processing usage, and just today found myself wondering which language processed faster. Thanks!!

  2. Thanks for dropping by.
    There are certain cases when you’ll really prefer processing data on the client-side. Although this speed evaluation might not be very accurate, we have a clue that AS3 is at least comparable with most server-side scripting langs.

  3. You are measuring MILLIseconds, not MICROseconds. If PHP really was 600 microseconds, then that is faster than AS3’s 50 or 150 millisecond time.

  4. I am measuring us not ms. Check the *1000 multiplication.

    And I redone this kinds of tests with Tamarin alone in various environments. The results were similar.