Is there any way to do std::vectorfaster on reserving + resizing?
I would like to achieve performance that would be somewhat equivalent to regular C arrays.
See the following code snippets:
TEST(test, vector1) {
for (int i = 0; i < 50; ++i) {
std::vector<int> a;
a.reserve(10000000);
a.resize(10000000);
}
}
TEST(test, vector2) {
for (int i = 0; i < 50; ++i) {
std::vector<int> a(10000000);
}
}
TEST(test, carray) {
for (int i = 0; i < 50; ++i) {
int* new_a = new int[10000000];
delete[] new_a;
}
}
The first two tests are twice as slow ( 4095 ms vs 2101 ms) and, obviously, this happens because it std::vectorresets the elements in it. Any ideas on how to avoid this?
Or maybe there is a standard (boost?) Container that implements an array with a fixed size and heap?
thank
source
share