C++ Array vs vector

前端 未结 8 1045
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 06:48

    Change assignment to eg. arr[i*size+j] = i*j, or some other non-constant expression. I think compiler optimizes away whole loop, as assigned values are never used, or replaces array with some precalculated values, so that loop isn't even executed and you get 0 milliseconds.

    Having changed 1 to i*j, i get the same timings for both vector and array, unless pass -O1 flag to gcc, then in both cases I get 0 milliseconds.

    So, first of all, double-check whether your loops are actually executed.

提交回复
热议问题