Here is another code to compare the efficiency of various ways to create 2D arrays dynamically.
Assume that we are reading data from an input ASCII file, that at the header line reports the number of rows (or outer_vector_size, or i-range) and at the beginning of each row reports the number of columns (or inner_vector_size, or j-range).
The 4 methods to be compared are:
1) a 2D dynamic array.
2) a vector<vector<int> >, that is resized before assigning the elements.
3) a vector<vector<int> >, without setting the size and just using push_back.
4) a vector<vector<int> >, that its capacity is set using reserve() before assigning the elements
Only the first two methods work so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <vector>
#include "timer.h"
using namespace std;
int main()
{
int outer_sz = 10000; // i-range
int inner_sz = 5000; // j-range
////////////////////////////////////////////////////////////
Timer timer;
timer.start();
cout << "1) a 2D dynamic array of size " << outer_sz << " x " << inner_sz << endl;
int** array2D;
array2D = new int*[outer_sz];
for (int i = 0; i < outer_sz; i++) {
array2D[i] = new int[inner_sz];
for (int j = 0; j < inner_sz; j++) {
array2D[i][j] = 1;
}
}
long elapsedMS = timer.stop();
cout << "Took " << elapsedMS << " ms." << endl;
for (int i = 0; i < outer_sz; i++)
delete[] array2D[i];
delete[] array2D;
////////////////////////////////////////////////////////////
timer.start();
cout << "2) a 2D vector of size " << outer_sz << " x " << inner_sz << ", using resize" << endl;
vector<vector<int> > vec2D;
vec2D.resize(outer_sz);
for (int i = 0; i < outer_sz; i++) {
vec2D[i].resize(inner_sz);
for (int j = 0; j < inner_sz; j++) {
vec2D[i][j] = 1;
}
}
elapsedMS = timer.stop();
cout << "Took " << elapsedMS << " ms." << endl;
////////////////////////////////////////////////////////////
timer.start();
cout << "3) a 2D vector of size " << outer_sz << " x " << inner_sz << ", using push_back" << endl;
vector<vector<int> > vec2D_2;
vec2D_2.push_back(vector<int> ());
for (int i = 0; i < outer_sz; i++) {
for (int j = 0; j < inner_sz; j++) {
vec2D_2[i].push_back(1);
}
}
elapsedMS = timer.stop();
cout << "Took " << elapsedMS << " ms." << endl;
////////////////////////////////////////////////////////////
timer.start();
cout << "4) a 2D vector of size " << outer_sz << " x " << inner_sz << ", using reserve" << endl;
vector<vector<int> > vec2D_3;
vec2D_3.reserve(outer_sz);
for (int i = 0; i < outer_sz; i++) {
vec2D_3[i].reserve(inner_sz);
for (int j = 0; j < inner_sz; j++) {
vec2D_3[i][j] = 1;
}
}
elapsedMS = timer.stop();
cout << "Took " << elapsedMS << " ms." << endl;
////////////////////////////////////////////////////////////
return 0;
}
|
and here is the output:
(it crashes in the middle:
2D_Vector.exe exited with code -1073741819)
1) a 2D dynamic array of size 10000 x 5000
Took 267 ms.
2) a 2D vector of size 10000 x 5000, using resize
Took 627 ms.
3) a 2D vector of size 10000 x 5000, using push_back |