Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
1.29/5 (3 votes)
See more:
Hi,


what is the performance penalty using std::vector?


Regards,
Ranjith
Posted
Comments
jeron1 5-Mar-14 13:14pm    
There shouldn't be much , if any, if used correctly. See this[^] discussion, it might help.
Sergey Alexandrovich Kryukov 5-Mar-14 14:13pm    
No, you please tell us: did you have any performance problem? :-)
—SA
[no name] 6-Mar-14 0:53am    
Performance penalty compared with what?

1. There is no performance issue until you have an actual application that runs too slowly.

2. There is no performance issue with std::vector until you have an application that runs too slowly, and can prove that std:: vector is taking up a significant amount of the processing time.

3. I am not aware of any such real world application suffering from that specific problem.

4. Caveat: nothing stops you from using std::vector inappropriately, or using std::vector when you should have been using e. g. std::list or std::map instead! In my above statements I am assuming you are using the type for data that don't require you to insert or remove elements in the midst of the array, at least not on a regular basis.
 
Share this answer
 
std::vector is ideal for insertions and deletions at the end of the collection and also for random read access.
The documentation talks more about it - vector Class[^]

Here are some excerpts from the documentation -

They should be the preferred container for a sequence when random-access performance is at a premium.

Vectors allow constant time insertions and deletions at the end of the sequence. Inserting or deleting elements in the middle of a vector requires linear time. The performance of the deque Class container is superior with respect to insertions and deletions at the beginning and end of a sequence. The list Class container is superior with respect to insertions and deletions at any location within a sequence.
 
Share this answer
 
hi,
I never faced any noticeable performance hits, but for a second opinion take a look at this link[^]
specially the part where 'reserve' is explained.

cheers!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900