Reputation: 16121
In a comparison on performance between Java and C++ our professor claimed, that theres no benefit in choosing a native array (dynamic array like int * a = new int[NUM]
) over std::vector. She claimed too, that the optimized vector can be faster, but didn't tell us why. What is the magic behind the vector?
PLEASE just answers on performance, this is not a general poll!
Upvotes: 1
Views: 226
Reputation: 18751
Any super optimized code with lower level stuff like raw arrays can beat or tie the performance of an std::vector
. But the benefits of having a vector far outweigh any small performance gains you get from low level code.
One thing I would keep in mind is that you shouldn't compare a dynamic array to a std::vector
they are two different things. It should be compared to something like std::dynarray
which unfortunately probably won't make it into c++14 (boost prolly has one, and I'm sure there are reference implementations lying around). A std::dynarray
implementation is unlikely to have any performance difference from a native array.
Upvotes: 3
Reputation: 51503
The only performance optimization a std::vector
can offer over a plain array is, that it can request more memory than currently needed. std::vector::reserve
does just that. Adding elements up to its capacity()
will not involve any more allocations.
This can be implemented with plain arrays as well, though.
Upvotes: 0
Reputation: 311088
Upvotes: 1