Adam
Adam

Reputation: 21

c++ std::vector performance [reference required]

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!

Upvotes: 2

Views: 295

Answers (4)

Klaim
Klaim

Reputation: 69682

The difference is usage and implementation relative.

Upvotes: 5

pmr
pmr

Reputation: 59811

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.

Upvotes: 0

Wyatt Anderson
Wyatt Anderson

Reputation: 9893

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.

Upvotes: 0

Puppy
Puppy

Reputation: 146920

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.

Upvotes: 3

Related Questions