Are Vectors (indexed arrays) always faster than Arrays?
Copy link to clipboard
Copied
When I say faster I mean less of a drain on performance.
Would an Vector be better than an Array when using an incursive array? Example:
var array:Array =
[[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 0, 2, 0, 3, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]];
Thanks!
Copy link to clipboard
Copied
When the data tends to be a built in type, not really. When you're using a class (as you are, an Array (class) of built in types), it can be faster. If your array is small and your calculations marginally complex it really won't make much difference on modern CPUs.
Copy link to clipboard
Copied
So it's not much faster when using built in data types that aren't classes? I'm asking for clarity because an Array is a built in type and yet you say that my example would benifit from being a Vector instead of an Array.
so something like this would be better:
var vector:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>();
But this wouldn't make much of a difference:
var vector:Vector.<uint> = new Vector.<uint>();
What about Objects and Points? Would those be better in a Vector or an Array?
Thanks!
Copy link to clipboard
Copied
You're reading me correctly but the Array class contains complex values, not primitive values. Types like Boolean, int, Number, String, etc. are faster due to primitive values being internally stored as immutable. Complex values are mutable and dynamic, like Array, Date, Error, Function, etc..
A Vector can speed up large amounts of calculations on objects with complex values. So yes, Objects that contain complex values such as a Point can speed up. However if you're literally not processing hundreds of thousands to millions of calculations the performance difference will be negligible on any modern CPU.
There's Adobe articles on simple benchmarks:
http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c3a0f5f19124318fc87b-7fff.html
And plenty of random tests from people performing large number iterations, posting performance differences. Some random link:
http://loteixeira.github.io/test/2013/02/16/array-vs-vector/
To accurately answer you, you really need to test a large number of iterations on the calculations and data YOU want to work with to see which makes more sense.
Copy link to clipboard
Copied
Where you really gain speed with a Vector is not having to cast or type-check things
Copy link to clipboard
Copied
Almost double performance in certain operations when you specify a fixed size for your Vector. All these things draw you closer to immutable performance.

