开发者

Initialization of vector of vector is very slow

I have a vector of vectors and I am trying to initialize it as follows:

vector<vector<float> > matrix(numberOfRows, vector<float> (numberOfCols));

But this line lasts about 0.89 where the numberOfRows and numberOfCols are:

const uint32_t numRows = 10000;
const uint32_t numCols = 20000;

Whereas initializing dynamics array with the following code take 0.04 sec to execute:

float **matrix = new float*[numberOfRows];
for (size_t i = 0; i < numberOfRows; ++i)
    matrix开发者_如何学运维[i] = new float[numberOfCols];

Am I doing something wrong is there any faster way to initialize that vector?

Edit:

As for the questions:

I used g++-4.5 to test using O3 optimization level with default standard.


The difference is that the vector is initialized to zero, while the dynamic array is not. With 200 million values, this is noticable.

You have the option of delaying initialization of the vector rows until you have real values to assign them, by not expanding the columns in the initialization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜