Speed accessing a std::vector by iterator vs by operator[]/index?

后端 未结 11 882
刺人心
刺人心 2020-12-03 04:42

Say, I have a

std::vector v;

in my code and I need to access its elements very often in the program, looping them forwa

11条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:02

    A simple loop-based benchmark has been fulfilled. I used VS 2010 SP1 (release configuration).

    1. Use iterators (*it = *it + 1;)
    2. Use indices (vs[i] = vs[i] + 1;)

    In several billions of iterations the second approach turned out to be a bit faster, by 1%. The result (indices are slightly faster than iterators) is reproducible but the difference, as I said, is very small.

提交回复
热议问题