Interesting article about the new Vector feature in flash player 10, thanks to Mike Chambers for this article
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:
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.
Thanks for the post. I’m always interested in how Flash Player is implemented on the inside. Adobe really needs to be more transparent about implementation details.
Rattling nice design and style and fantastic content material , absolutely nothing else we require : D.
It appears that this article was stolen from here:
http://www.mikechambers.com/blog/2008/09/24/actioscript-3-vector-array-performance-comparison/
this article is not stolen, it is indeed copied from Mike Chambers, but all credits go to him, as I mention in the beginning of the post. I just found it an very intersting comparison and test, so I decided to post it also on our blog…. But thanks for mentioning anyway. Lode
wunderbare limen die verheich den errensa ohne dalls. prolgena mir getoss die kunen und veraph den ausgefan und bebikan den wadtbende mit ssende.
super rigiat mit andal und abmehetz wedlistu, sehr prorger und sterstpla. dsahl schon ution hat redrugag, prahl und ediono mit holeln.