Amazingly, I’ve never done a straight shootout between Vector and Array on this site. It’s a simple test, so read right on to see what kind of added performance you can get by using Flash 10’s Vector.

Intro

First of all, a little game. Ask yourself four questions before you read on to the performance results below:

  1. How much faster is Vector than Array when reading elements?
  2. How about when writing elements?
  3. What kind of performance speed up do you get by setting the Vector to a fixed-length?
  4. Is the speedup different on Mac compared to Windows?

Let’s look at the test that answers all these questions:

Test App
package
{
	import flash.text.*;
	import flash.utils.*;
	import flash.display.*;
 
	public class ArrayVsVector extends Sprite
	{
		public function ArrayVsVector()
		{
			var logger:TextField = new TextField();
			logger.autoSize = TextFieldAutoSize.LEFT;
			addChild(logger);
			function log(msg:*): void { logger.appendText(msg+"\n"); }
 
			const REPS:int = 1000;
			const SIZE:int = 100000;
			const VAL:int = 42;
			var i:int;
			var j:int;
			var temp:int;
			var beforeTime:int;
			var afterTime:int;
 
			var array:Array = new Array(SIZE);
			var vectorFixed:Vector.<int> = new Vector.<int>(SIZE, true);
			var vectorDynamic:Vector.<int> = new Vector.<int>(SIZE, false);
			for (i = 0; i < SIZE; ++i)
			{
				array[i] = VAL;
				vectorFixed[i] = VAL;
				vectorDynamic[i] = VAL;
			}
 
			log("Read:");
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				for (j = 0; j < SIZE; ++j)
				{
					temp = array[j];
				}
			}
			afterTime = getTimer();
			log("\tArray: " + (afterTime-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				for (j = 0; j < SIZE; ++j)
				{
					temp = vectorFixed[j];
				}
			}
			afterTime = getTimer();
			log("\tVector (fixed): " + (afterTime-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				for (j = 0; j < SIZE; ++j)
				{
					temp = vectorDynamic[j];
				}
			}
			afterTime = getTimer();
			log("\tVector (dynamic): " + (afterTime-beforeTime));
 
			log("Write:");
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				for (j = 0; j < SIZE; ++j)
				{
					array[j] = VAL;
				}
			}
			afterTime = getTimer();
			log("\tArray: " + (afterTime-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				for (j = 0; j < SIZE; ++j)
				{
					vectorFixed[j] = VAL;
				}
			}
			afterTime = getTimer();
			log("\tVector (fixed): " + (afterTime-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				for (j = 0; j < SIZE; ++j)
				{
					vectorDynamic[j] = VAL;
				}
			}
			afterTime = getTimer();
			log("\tVector (dynamic): " + (afterTime-beforeTime));
		}
	}
}
Performance Results

Now to check your assumptions with the performance results:

Environment Array (read) Vector (fixed, read) Vector (dynamic, read) Array (write) Vector (fixed, write) Vector (dynamic, write)
3.0 Ghz Intel Core 2 Duo, Windows XP 835 599 595 1912 698 714
2.0 Ghz Intel Core 2 Duo, Mac OS X 10.5 1743 1083 1083 3390 1204 1221

Here are the speedups by percentage:

Environment Array (read) Vector (fixed, read) Vector (dynamic, read) Array (write) Vector (fixed, write) Vector (dynamic, write)
3.0 Ghz Intel Core 2 Duo, Windows XP 39% 40% 174% 168%
2.0 Ghz Intel Core 2 Duo, Mac OS X 10.5 61% 61% 182% 178%
Performance Analysis

Now let’s revisit those questions:

  1. How much faster is Vector than Array when reading elements?
    40% on Windows, 60% on Mac.
  2. How about when writing elements?
    170-180%.
  3. What kind of performance speed up do you get by setting the Vector to a fixed-length?
    None, sadly.
  4. Is the speedup different on Mac compared to Windows?
    Yes, the reading speedup is 50% greater on Mac than on Windows.
Conclusion

The Vector class yields a nice speedup where it counts: reading and writing elements. Aside from some quirky scenarios, you should always prefer Vectors over Arrays in performance-critical code. So, how’d you do on the little quiz? Sound off in the comments.