In our C++ course they suggest not to use C++ arrays on new projects anymore. As far as I know Stroustroup himself suggests not to use arrays. But are there significant performance differences?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Numpy matrix of coordinates
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
The performance difference between the two is very much implementation dependent - if you compare a badly implemented std::vector to an optimal array implementation, the array would win, but turn it around and the vector would win...
As long as you compare apples with apples (either both the array and the vector have a fixed number of elements, or both get resized dynamically) I would think that the performance difference is negligible as long as you follow got STL coding practise. Don't forget that using standard C++ containers also allows you to make use of the pre-rolled algorithms that are part of the standard C++ library and most of them are likely to be better performing than the average implementation of the same algorithm you build yourself.
That said, IMHO the vector wins in a debug scenario with a debug STL as most STL implementations with a proper debug mode can at least highlight/cathc the typical mistakes made by people when working with standard containers.
Oh, and don't forget that the array and the vector share the same memory layout so you can use vectors to pass data to legacy C or C++ code that expects basic arrays. Keep in mind that most bets are off in that scenario, though, and you're dealing with raw memory again.
Go with STL. There's no performance penalty. The algorithms are very efficient and they do a good job of handling the kinds of details that most of us would not think about.
If you compile the software in debug mode, many compilers will not inline the accessor functions of the vector. This will make the stl vector implementation much slower in circumstances where performance is an issue. It will also make the code easier to debug since you can see in the debugger how much memory was allocated.
In optimized mode, I would expect the stl vector to approach the efficiency of an array. This is since many of the vector methods are now inlined.
There is definitely a performance impact to using an
std::vector
vs a raw array when you want an uninitialized buffer (e.g. to use as destination formemcpy()
). Anstd::vector
will initialize all its elements using the default constructor. A raw array will not.The c++ spec for the
std:vector
constructor taking acount
argument (it's the third form) states:A raw array does not incur this initialization cost.
See also How can I avoid std::vector<> to initialize all its elements?
You have even fewer reasons to use plain arrays in C++11.
There are 3 kind of arrays in nature from fastest to slowest, depending on the features they have (of course the quality of implementation can make things really fast even for case 3 in the list):
std::array<T, N>
dynarray
in C++ TS after C++14. In C there are VLAsstd::vector<T>
For 1. plain static arrays with fixed number of elements, use
std::array<T, N>
in C++11.For 2. fixed size arrays specified at runtime, but that won't change their size, there is discussion in C++14 but it has been moved to a technical specification and made out of C++14 finally.
For 3.
std::vector<T>
will usually ask for memory in the heap. This could have performance consequences, though you could usestd::vector<T, MyAlloc<T>>
to improve the situation with a custom allocator. The advantage compared toT mytype[] = new MyType[n];
is that you can resize it and that it will not decay to a pointer, as plain arrays do.Use the standard library types mentioned to avoid arrays decaying to pointers. You will save debugging time and the performance is exactly the same as with plain arrays if you use the same set of features.
Using C++ arrays with
new
(that is, using dynamical arrays) should be avoided. There is the problem you have to keep track of the size, and you need to delete them manually, and do all sort of housekeeping.Using arrays on the stack is also discouraged because you don't have range checking, and passing the array around will lose any information about its size (array to pointer conversion). You should use
boost::array
in that case, which wraps a C++ array in a small class and provides asize
function and iterators to iterate over it.Now the std::vector vs. native C++ arrays (taken from the internet):
Note: If you allocate arrays with
new
and allocate non-class objects (like plainint
) or classes without a user defined constructor and you don't want to have your elements initialized initially, usingnew
-allocated arrays can have performance advantages becausestd::vector
initializes all elements to default values (0 for int, for example) on construction (credits to @bernie for remembering me).