FP10: Vector vs Array
Update
I have replaced the Sprite by an own class/object (SimpleObject with 3 public properties, x, y, z). The results are the same.
Vector vs Array
Started a little speed measurment with new Vector-type and the "old-fashioned" array. As everyone could read in the Adobe AS3 Docs, Vector should give us some advantages over Array iteration and data-access.
I have tried several test-settings and i was completely surprised by the results (in a bad way).
What i am most interested in, is fast access of objects. So i create a pool of object-references(simple sprite), one as an array and the other pool is defined as a Vector.<Sprite>.
-
var i:uint = 0;
-
var n:uint = N_TEST; ( n = 20000000)
-
var v:Vector.<Sprite>;
-
v = this.objectPoolV;
-
-
var timestamp:int = getTimer();
-
-
i = 0;
-
var r:Sprite;
-
while (i <n)
-
{
-
r = v[i];
-
-
i++;
-
}
-
-
result.htmlText += "<br />Result Vector Complex Operation Test: " + (getTimer() - timestamp);
This was my first test and the difference is noticeable (but look at n, how high it has to be for even get a small difference).
- Vector result: 511 ms
- Array result: 659 ms
But this is not a very commen use. So, next step was to save the reference in a locale variable and move the sprite in all 3 directions about 10px. I have done this test with and without an explicit cast.
-
var i:uint = 0;
-
var n:uint = N_TEST;
-
var v:Array;
-
var r:Sprite;
-
v = this.objectPoolA;
-
-
i = 0;
-
-
var timestamp:int = getTimer();
-
while (i <n)
-
{
-
r = v[i];
-
r.x += 100;
-
r.y += 100;
-
r.z += 100;
-
-
i++;
-
}
-
-
result.htmlText += "<br />Result Array Complex Operation Test: " + (getTimer() - timestamp);
This result i have never expected, array is faster!! than the vector, but why? I am only referencing to the current dataset.
- Vector result: 5118 ms
- Array result: 4885 ms
If i cast the array, the result is worse than without a cast.
-
r = Sprite(v[i]);
I know, casting is expensive, but look at the results with casting.
- Vector result: 7055 ms
- Array result: 7126 ms
So, finally let's have a look at the worst sample. Direct access of properties threw the Array and Vector.
-
var i:uint = 0;
-
var n:uint = N_TEST;
-
var v:Vector.<Sprite>;
-
v = this.objectPoolV;
-
i = 0;
-
var timestamp:int = getTimer();
-
while (i <n)
-
{
-
v[i].x += 100;
-
v[i].y += 100;
-
v[i].z += 100;
-
i++;
-
}
-
result.htmlText += "<br />Result Vector Complex Operation Test: " + (getTimer() - timestamp);
I have to reduce the N_TEST constant to 1000000, here are the results
- Vector result: 1751 ms
- Array result: 1631 ms
Is anyone able to confirm this results?
The complete testfile is downloadable here. (If you find a mistake, please tell me).
About this entry
You’re currently reading “FP10: Vector vs Array,” an entry on Hannes Moser
- Published:
- 18.06.08 / 2pm
- Category:
- Flash Platform










6 Comments
Jump to comment form | comments rss [?] | trackback uri [?]