Mike Chambers

code = joy

ActioScript 3 Vector / Array Performance Comparison

with 11 comments

In my original post on the new Flash Player 10 Vector class, I did a simple example that showed Vectors being slightly faster than Array when just populating and looping through collections.

Below is another example that shows a more significant performance increase when using Vectors. In this example, I populate an Array and Vector with 5 million random numbers, and then loop through them and average all of the numbers.

Running the test on my MacBook Pro, the Vector is about 60% faster:

Vector 1.824
Array 2.938
62.08 %

Here is the code:

package {
	import flash.display.Sprite;
	import __AS3__.vec.Vector;

	public class VectorPerformance extends Sprite
	{
		private static const LENGTH:int = 5000000;

		public function VectorPerformance()
		{
			//vector performance
			var vectorStartTime:Number = getTime();

			var v:Vector.<Number> = generateVector();
			var vAvg:Number = averageVector(v);

			var vectorTime:Number = (getTime() - vectorStartTime) / 1000;

			//array performance
			var arrayStartTime:Number = getTime();

			var a:Array = generateArray();
			var aAvg:Number = averageArray(a);

			var arrayTime:Number = (getTime() - arrayStartTime) / 1000;

			trace("Vector", vectorTime);
			trace("Array", arrayTime);
			trace((vectorTime/arrayTime) * 100 + " %");
		}

		private function generateVector():Vector.<Number>
		{
			var v:Vector.<Number> = new Vector.<Number>(LENGTH, true);

			for(var i:int = 0; i < LENGTH; i++)
			{
				v[i] = Math.random() * 100000;
			}

			return v;
		}

		private function averageVector(v:Vector.<Number>):Number
		{
			var sum:Number = 0;
			var len:int = v.length;

			for(var i:int = 0; i < LENGTH; i++)
			{
				sum += v[i];
			}

			return (sum / len);
		}

		private function generateArray():Array
		{
			var a:Array = new Array(LENGTH);

			for(var i:int = 0; i < LENGTH; i++)
			{
				a[i] = Math.random() * 100000;
			}

			return a;
		}

		private function averageArray(arr:Array):Number
		{
			var sum:Number = 0;
			var len:int = arr.length;

			for(var i:int = 0; i < LENGTH; i++)
			{
				sum += arr[i];
			}

			return (sum / len);
		}		

		private function getTime():Number
		{
			return (new Date()).getTime();
		}
	}
}

Of course, this is only one test. Depending on what you are doing, Vectors may be even faster, or slower.

One interesting note, originally my test had each collection sorted before they were averaged, but it turned out that sorting Vector was slower than sorting the Array. Not sure if this is a bug, but I have reported it to the player team.

Written by mikechambers

September 24th, 2008 at 4:48 pm

Posted in General

Tagged with

11 Responses to 'ActioScript 3 Vector / Array Performance Comparison'

Subscribe to comments with RSS or TrackBack to 'ActioScript 3 Vector / Array Performance Comparison'.

  1. 40% faster :)

    magicwind

    24 Sep 08 at 6:27 pm

  2. [...] Chambers has posted a comparison between the performance of AS3 arrays and the new Vectors available in Flash Player 10. The results [...]

  3. How generics used with vector affects the test?

    radekg

    25 Sep 08 at 11:59 am

  4. [...] Aqui el codigo fuente [...]

  5. Why my vector use source is need more memory?

    private var vector:Vector. = new Vector.;
    private var array:Array = new Array();

    private function init():void
    {

    var start:Number = new Date().getTime();

    for(var i:Number = 0 ; i < 5000000 ; i++)
    {
    array[i] = Math.random() * Math.PI * 100000;
    //vector[i] = Math.random() * Math.PI * 100000;
    }

    trace(System.totalMemory);
    trace(new Date().getTime() – start);
    }

    Vector
    71843840
    2782

    Array
    15237120
    2859

    mash

    30 Sep 08 at 10:40 pm

  6. [...] reports have been coming in across the web of performance increase anywhere between 40% to 60% (AS3 Vector / Array Performance Comparison). However, this is one small step for the platform as a whole as with some of the benchmarks I have [...]

  7. Are there built-in comparators for sorting Vectors in Flash Player 10? I need to alphabetically sort a Vector based on the return value of a method.

    TK

    24 Oct 08 at 5:24 pm

  8. [...] Vector is an interesting idea.  It adds type-safe collections to ActionScript, and it provides a pretty dramatic speed increase since it side-steps things like type-checking.  But Vector does not provide a generic typing [...]

  9. Just ran across an interesting performance comparison tool that shows Vectors as 60x faster when looping through them.
    http://businessintelligence.me/projects/performance_tester/performanceTester.html

    PS: Actio script?

    Mims H. Wright

    17 Dec 08 at 11:40 am

  10. [...] ActioScript 3 Vector / Array Performance Comparison [...]

  11. [...] with an Array which holds all the same DataType. Vectors are approximately 40% faster according to Mike Chambers to Arrays. When dealing with a large Array that is the same type, use a Vector. Also note that [...]

Leave a Reply