PDA

View Full Version : To "Vector" or to "Array", that is the question...



IQAndreas
January 22nd, 2010, 06:54 PM
I'm developing a small library and trying to outweigh the pros and cons of using Vector vs. Array.

So far, I can only think of a few things to think of:

Vector only works for FP10, while Array works for both 9 and 10.

Vector is typed (which is a wonderful, fabulous thing), but if I'm only using the Array as a private variable, so I have full control over what goes in and comes out of it, this factor may not play as great a part anymore, but definitely great protection.


I believe I heard somewhere that Vector is a lot faster than Array, yet in a quick inconclusive speed test I did a while back, it didn't seem to make any real difference at all.

Is Vector really faster than Array? If so, is it faster in all aspects, or only faster in certain situations just as uint is only faster than Number when using byteshifting?

rumblesushi
January 22nd, 2010, 07:03 PM
Pretty simple, using complex types the Array is generally faster. Yes that's right, not the same speed, faster.

Using floats or integers, the Vector is a fair bit quicker.

If you have an array of numbers, use Vectors. If you have an array of classes/objects, use Arrays.

Krilnon
January 22nd, 2010, 07:29 PM
FWIW, there are individual C++ classes used for the implementations of each of the different types that rumblesushi mentioned would benefit from being contained in a Vector instead of an Array. So, it makes sense that those particular types would be quicker when used in a Vector, since there are pretty obvious optimizations that can be made for primitive types.

There are literally special cases for those three:
if (so == toplevel()->intClass)
return toplevel()->intVectorClass->atom();
else if( so == toplevel()->numberClass )
return toplevel()->doubleVectorClass->atom();
else if( so == toplevel()->uintClass )
return toplevel()->uintVectorClass->atom();