Rares Dima
Rares Dima

Reputation: 1757

Matrix/Vector initialization performance

This is more of an educational question, there is no specific problem I am trying to solve. I would like some insight into what happens "behind the scenes" in the following scenario:

We have 2 ints, w and h, and we need a matrix (vector<vector<int>>) of 0s. There are multiple ways to do this and I would like to know which one performs best (probably this means which one performs the least copies).

Option 1:

vector<vector<int>> m;

for (int i = 0; i < h; i++)
{
    m.push_back(vector<int>());
    for (j = 0; j < w; j++)
        m[i].push_back(0);
}

Option 2:

vector<vector<int>> m;

for (int i = 0; i < h; i++)
    m.push_back(vector<int>(w, 0));

Option 3:

vector<vector<int>> m(h, vector<int>(w, 0));

Is the temporary value at m.push_back(vector<int>()); / m.push_back(vector<int>(w, 0)); created in memory and then also copied into m? If it is wouldn't it be better to use option 1 to minimize copying? (suppose we are only talking about larger arrays, say 1,000,000 x 1,000,000). Same dilemmas for option 3; which tends to be faster (at least on paper) and why would it be?

Upvotes: 0

Views: 65

Answers (1)

MSalters
MSalters

Reputation: 179907

If you want performance for a Matrix class, you don't use std::vector<std::vector<T>> in the first place. You write a proper class that encapsulates a one-dimensional std::vector<T>. A vector of vectors is fragmented in memory.

A one-trillion element matrix is technically possible on commercial hardware nowadays, but to initialize it you really, really want multiple threads. That's another practical objection against your 3 examples.

Having said that, for small experiments all your code is fine.

Upvotes: 1

Related Questions