C++ Array vs vector

前端 未结 8 996
伪装坚强ぢ
伪装坚强ぢ 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:39

    If you are compiling this with a Microsoft compiler, to make it a fair comparison you need to switch off iterator security checks and iterator debugging, by defining _SECURE_SCL=0 and _HAS_ITERATOR_DEBUGGING=0.

    Secondly, the constructor you are using initialises each vector value with zero, and you are not memsetting the array to zero before filling it. So you are traversing the vector twice.

    Try:

    vector v; 
    v.reserve(size*size);
    

提交回复
热议问题