I’m creating a huge matrix that is stored inside nested vectors:
typedef vector<vector<pair<unsigned int, char>>> Matrix;
The outer vector will eventually contain ~400.000 vectors that which each contain ~220 pairs at max (most contain less). This takes about 1GB of RAM and is done like this:
Matrix matrix;
for (unsigned int i = 0; i < rows; i++) {
vector<pair<unsigned int, char>> row;
for (unsigned int j = 0; j < cols; j++) {
// ...calculations...
row.push_back( pair<unsigned int, char>(x, y) );
}
matrix.push_back(row);
}
The first 20% go quite fast but the larger the outer vector grows, the slower gets the whole process. I’m pretty sure that there is some optimization possible, but I’m not an expert on this field. Are there any simple tricks to speed this up? Or are there any major faults in my attempt?
It would be better to just use a single one dimensional vector and wrap up the row, column indexing in some functions/class. This way the memory for the entire matrix is guaranteed to be contiguous.
And instead of using
push_backallocate the entire matrix up front: