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>.

Actionscript:
  1. var i:uint = 0;
  2. var n:uint = N_TEST; ( n = 20000000)
  3. var v:Vector.&lt;Sprite&gt;;
  4. v = this.objectPoolV;
  5.  
  6. var timestamp:int = getTimer();
  7.  
  8. i = 0;
  9. var r:Sprite;
  10. while (i &lt;n)
  11. {
  12. r = v[i];
  13.  
  14. i++;
  15. }
  16.  
  17. result.htmlText += "&lt;br /&gt;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.

Actionscript:
  1. var i:uint = 0;
  2. var n:uint = N_TEST;
  3. var v:Array;
  4. var r:Sprite;
  5. v = this.objectPoolA;
  6.  
  7. i = 0;
  8.  
  9. var timestamp:int = getTimer();
  10. while (i &lt;n)
  11. {
  12. r = v[i];
  13. r.x += 100;
  14. r.y += 100;
  15. r.z += 100;
  16.  
  17. i++;
  18. }
  19.  
  20. result.htmlText += "&lt;br /&gt;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.

Actionscript:
  1. 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.

Actionscript:
  1. var i:uint = 0;
  2. var n:uint = N_TEST;
  3. var v:Vector.&lt;Sprite&gt;;
  4. v = this.objectPoolV;
  5. i = 0;
  6. var timestamp:int = getTimer();
  7. while (i &lt;n)
  8. {
  9. v[i].x += 100;
  10. v[i].y += 100;
  11. v[i].z += 100;
  12. i++;
  13. }
  14. result.htmlText += "&lt;br /&gt;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