I'm writing a parallel implemen开发者_如何学编程tation 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!
The difference is usage and implementation relative.
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.
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.
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::vector
if you callvector::at
which cannot happen with raw pointers.
精彩评论