By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

The performance cost associated with std::vector.

P: n/a
What harmful effects can using std::vector have on the performance of my
program? How do I protect my program from these effects? Thanks.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mon, 15 Dec 2003 00:58:50 +1100, "Jason Heyes"
<ge******@optusnet.com.au> wrote:
What harmful effects can using std::vector have on the performance of my
program? How do I protect my program from these effects? Thanks.


I think a more appropriate question would be:

"What harmful effects can NOT using std::vector have on the
performance of my program? How do I protect my program from these
effects?"
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2

P: n/a
"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...
What harmful effects can using std::vector have on the performance of my
program? How do I protect my program from these effects? Thanks.


Performance compared with what?

Jul 22 '05 #3

P: n/a
"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...
What harmful effects can using std::vector have on the performance of my
program? How do I protect my program from these effects? Thanks.

On most implementations, perfomance is similar to an array, but it should
not be hard to code a little test to find out how your particular
implementation measures up... The advantages of using a vector (like safety
from memory management problems and the like) far outweigh any perfomance
penalties for all but the most time-critical applications.
HTH,
S. Armondi
Jul 22 '05 #4

P: n/a
On Mon, 15 Dec 2003 00:58:50 +1100, "Jason Heyes"
<ge******@optusnet.com.au> wrote:
What harmful effects can using std::vector have on the performance of my
program? How do I protect my program from these effects? Thanks.


Compared against dynamic allocation using malloc/new[], you really
only have minor concerns that I can think of.

1. Vector elements are initialized when you resize. You can avoid this
where it has caused a performance problem by using push_back to add
elements (having reserved enough space).

2. Avoid copying vectors (or passing by value) unless you really want
to perform a deep copy.

Apart from that, performance should be close to identical to a
hand-coded hand-optimized malloc-based array using exponential
expansion.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.