I'm writing a parallel implementation of some data structures. I'd like to find out if someone know about difference in performance between pure pointers and std::vector. If you know trustful docs about it, please write URL/book name/whatever. Any hints are welcome!
4 Answers
The difference is usage and implementation relative.
2 Comments
std::vector<> is just a friendly dynamically allocated array.You can make std::vector as fast as normal pointers by using the unchecked operator[] and resizing appropriately. The reality is that vector is a compile-time abstraction over a pointer- not a runtime one, unless you choose to use extras. What's much more important is the vastly increased safety vector offers- debugging iterators, automatic and safe resource management, etc. There is no reason to use a raw pointer.
Edit: My reference is that profiling run you did before you even considered losing the safety of vector.
Comments
According to this answer in a similar question, accessing an element in a dynamically-allocated array versus a std::vector will be roughly the same. There's some good analysis in that question and in this one as well.
Comments
If you mean to compare a std::vector with some hand-written dynamic array here are some points of reference:
- The resizing factor on insertion is important. This factor isn't specified by the standard but is usually between 1.5 or 2 and it must guarantee amortized constant time on insertion operations.
- The allocator: A lot of the performance depends on the allocation mechanism that is used, same goes for your pointers.
- Boundary checking can occur in
std::vectorif you callvector::atwhich cannot happen with raw pointers.
3 Comments
vector.vector dynamic resizing is a must. If he doesn't need dynamic resizing he should use std::array anyway.
std::vector?n>=0objects of the same type. What do you mean exactly?