C++ Array vs vector

前端 未结 8 1049
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 06:06

when using C++ vector, time spent is 718 milliseconds, while when I use Array, time is almost 0 milliseconds.

Why so much performance difference?

int         


        
8条回答
  •  春和景丽
    2021-01-03 06:50

    You are probably using VC++, in which case by default standard library components perform many checks at run-time (e.g whether index is in range). These checks can be turned off by defining some macros as 0 (I think _SECURE_SCL).

    Another thing is that I can't even run your code as is: the automatic array is way too large for the stack. When I make it global, then with MingW 3.5 the times I get are 627 ms for the vector and 26875 ms (!!) for the array, which indicates there are really big problems with an array of this size.

    As to this particular operation (filling with value 1), you could use the vector's constructor:

    std::vector v(size * size, 1);
    

    and the fill algorithm for the array:

    std::fill(arr, arr + size * size, 1);
    

提交回复
热议问题