Is there a performance reason why a vector would be initialized with size?
For example vector<myClass> v(10);
as opposed to vector<myClass> v
and push_back as needed?
Is there a performance reason why a vector would be initialized with size?
For example vector<myClass> v(10);
as opposed to vector<myClass> v
and push_back as needed?
vector<myClass> v(10)
pre-allocates the internal array once time and auto-fills it with 10 default-constructed myClass
objects.
vector<myClass> v
does not pre-allocate the array, but you can use reserve()
and resize()
for that.
push_back()
will re-allocate and copy the internal array each time that the new size()
would exceed the current capacity()
.
If the push_back()
grows the vector past its current capacity()
, it will reallocate its array, something that is not efficient.
So, if you tell the vector exactly how many elements you are going to store, then you do not pay the cost of re-allocating the vector, especially if the vector has to change location in the memory (because it does not fit where it is), thus it copies itself (additional cost!).
But should you believe me? Maybe. You should however believe the facts:
Test code
#include <vector>
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
using namespace std;
int main() {
const int N = 1000000;
using namespace std::chrono;
{
// push_back only
high_resolution_clock::time_point t1 = high_resolution_clock::now();
vector<int> v1;
for(int i = 0; i < N; ++i)
v1.push_back(i);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took me " << time_span.count() << " seconds.";
std::cout << std::endl;
}
{
// set size and use operator []
high_resolution_clock::time_point t1 = high_resolution_clock::now();
vector<int> v2(N);
for(int i = 0; i < N; ++i)
v2[i] = i;
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took me " << time_span.count() << " seconds.";
std::cout << std::endl;
}
return 0;
}
Output
It took me 0.0170491 seconds.
It took me 0.00236058 seconds.
As you can see, the experimental results show that by setting the size of the vector, we are faster by one magnitude.
My source for time measurements.