Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it more efficient to preallocate a vector?

Tags:

c++

stl

In C++ Primer fourth edition, by Stanley B.Lippman, Josee Lajoie and Barbara E. Moo it states:

Because vectors grow efficiently, it is usually best to let the vector grow by adding elements to it dynamically as the element values are known.

and

Readers accustomed to using c or java might expect that because vector elements are stored contiguously, it would be best to preallocate the vector at its expected size. In fact the contrary is the case...

and

Allthough we can preallocate a given number of elements in a vector, it is usually more efficient to define an empty vector and add elements to it.

Assuming this is correct (the authors are as reputable as they come, one is a co-author of C++ itself) then can anyone give me a case that proves this statement, and explain why?

like image 201
dangerousdave Avatar asked Aug 09 '12 16:08

dangerousdave


People also ask

Is array more efficient than vector?

Array is memory efficient data structure. Vector takes more time in accessing elements. Array access elements in constant time irrespective of their location as elements are arranged in a contiguous memory allocation.

Why are sets faster than vectors?

The time complexity for the insertion of a new element is O(log N). Vector is faster for insertion and deletion of elements at the end of the container. Set is faster for insertion and deletion of elements at the middle of the container.

Is vector faster than set C++?

Using a sorted vector instead of a set gives you faster lookup and much faster iteration, but at the cost of slower insertion. Insertion into a set, using set::insert, is proportional to log N, but insertion into a sorted vector, using insert_into_vector, is proportional to N.

How do you increase the capacity of a vector?

To achieve this the vector uses an internal array. To improve performance a larger array (as needed) is allocated (thats the capacity). The push_back new has one job: to add a new element at the end of the vector (see last line of push_back). But sometimes the internal array is already full.


2 Answers

It depends.

If you don't know what the final size will be, then let the vector allocate using its allocation scheme (usually doubles each time, or somewhere around there). This way you avoid reallocating for every single element:

std::vector<int> v;  // good: for (/* populate v */) // unknown number of iterations {     v.push_back(i); // possible reallocation, but not often }  // bad: for (/* populate v */) // unknown number of iterations {     v.reserve(v.size() + 1); // definite reallocation, every time     v.push_back(i); // (no reallocation) } 

But if you know ahead of time you won't be reallocating, then preallocate:

std::vector<int> v;  // good: v.reserve(10);  for (/* populate v */) // only 10 iterations (for example) {     v.push_back(i); // no reallocations }  // not bad, but not the best: for (/* populate v */) // only 10 iterations (for example) {     v.push_back(i); // possible reallocation, but not often (but more than needed!) } 
like image 110
GManNickG Avatar answered Sep 20 '22 20:09

GManNickG


I timed this simple example:

#include<iostream> #include<vector>  int main() {      int limit = 100 * 1000 * 1000;     std::vector<long> my_vec;     my_vec.reserve(limit); // comment out this line to not preallocate      for (int i=0; i < limit; i++) {         my_vec.push_back(i);     }      long my_sum = 0;     for (int i=0; i < limit; i++) {         my_sum += my_vec[i];     }      std::cout << my_sum << std::endl;     return 0; } 

Complied with:

g++ -std=c++11 -O2 my_file.cpp -o my_exec 

And found the difference to be substantial:

Without preallocation:

real    0m3.366s user    0m1.656s sys     0m1.660s 

With preallocation:

real    0m1.688s user    0m0.732s sys     0m0.936s 

My conclusion here is: If building a vector is a big part of the program, then preallocating for efficiency makes sense. However, building a larger vector over and over is unlikely, and thus it is rarely a bottle neck. However, using reserve() has other advantages besides preallocating.

Bjarne Stroustrup in The C++ programming language (4th addition) has this to say:

I used to be careful about using reserve() when I was reading into a vector. I was surprised to find that for essentially all my uses, calling reserve() did not measurably affect performance. The default growth strategy worked just as well as my estimates, so I stopped trying to improve performance using reserve(). Instead I use it to increase predictability of reallocation delays and to prevent invalidation of pointers and iterators.

like image 36
Akavall Avatar answered Sep 18 '22 20:09

Akavall