2

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!

8
  • 1
    What is the commonality between pointers and std::vector? Commented Nov 25, 2010 at 21:14
  • Unfortunately I know nothing about this, but +1 for a very good, probably very common question :-) Commented Nov 25, 2010 at 21:15
  • Both can store any kind of data. I ask cause I don't know which one is faster. Commented Nov 25, 2010 at 21:15
  • I assume that by "pure pointers" you mean hand-crafted dynamically allocated arrays? Commented Nov 25, 2010 at 21:17
  • @Adam: no, they can't hold "any kind of data". The can hold n>=0 objects of the same type. What do you mean exactly? Commented Nov 25, 2010 at 21:18

4 Answers 4

5

The difference is usage and implementation relative.

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

2 Comments

+1: std::vector<> is just a friendly dynamically allocated array.
I "hid" the developpement of this answer in "history", but the initial sentence should be enough for most of us.
3

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

0

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

0

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 call vector::at which cannot happen with raw pointers.

3 Comments

the resizing factor isn't relevant if you're making an apples to apples comparison. A raw array has to be sized up front, so presumably you'd do the same with vector.
@jalf: it's not really clear what "pure pointers" means, so it's more of an apples to some-unspecified-fruit-in-a-locked-box comparison.
@jalf: I thought if he compared something with vector dynamic resizing is a must. If he doesn't need dynamic resizing he should use std::array anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.