1

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!

10
  • Did you consider asking your professor for her reasoning? Commented Nov 11, 2013 at 23:01
  • 2
    You contradict yourself in your own question Commented Nov 11, 2013 at 23:02
  • Also, I would point out that this is flatly incorrect: "theres no benefit in choosing std::vector over a native array." A vector will manage the array allocation for you, so there is a very straightforward benefit in that it's much harder to leak the array allocation by using a vector. Commented Nov 11, 2013 at 23:03
  • 1
    @cdhowie right. but I'm talking about performance. Neither safety nor convenience. Commented Nov 11, 2013 at 23:05
  • 1
    It's hard to believe that a vector could be faster than an array, as fast perhaps but not faster. In any case it's the wrong consideration, performance issues are normally totally irrelevant when making this choice. Safety, convenience and functionality are much more important. Commented Nov 11, 2013 at 23:09

3 Answers 3

3

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.

  1. vector manages it's own memory, array you have to remember to manage the memory
  2. vector allows you to make use of stdlib more easily (with dynamic arrays you have to keep track of the end ptr yourself) which is all very well written well tested code
  3. sometimes vector can even be faster, like qsort v std::sort, read this article, keep in mind the reason this can happen is because writing higher level code can often allow you to reason better about the problem at hand.
  4. Since the std containers are good to use for everything else, that dynamic arrays don't cover, keeping code consistent in style makes it more readable and less prone to errors.

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Your second point is also true of arrays. Both are considered to have random-access iterators.
@chris true but with dynamic arrays you have to keep track of the end ptr yourself
So I reread the OP's question a couple of times: Who said anything about dynamic arrays?
@WhozCraig I assumed that is what he was referring to by native arrays
Apparently everyone else does as well, but hopefully the distinction is obviously important. The truly "native" arrays offered up purely by the language are of the form Type var[N]; and Type var[] = {initializer list}; and for construction you simply cannot beat sub esp, N*sizeof(Type) on the allocation side. Once construction is done, they're on equal ground (obviously).
|
1
  1. There is benefit of using vector instead of array when the number of elements needs to be changed.
  2. Neither optimized vector can be faster than array.

Comments

0

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.

2 Comments

I can allocate a native array bigger than I need, a.k.a. reserving.
@Thomas I guess that's why I noted that this can be done with arrays as well. If you feel the need to comment, don't just parrot what has already been said.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.