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

后端 未结 11 865
刺人心
刺人心 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 04:56

    You are not only prematurely optimizing, you are micro-optimizing. This is an evil almost as bad as the former (the difference being that very, very, very rarely it is actually necessary to micro-optimize). Put the two together and you've got a recipe for disaster.

    If you run a profiler and find this area of code is a bottleneck then you will need to optimize. You don't optimize by trying to reduce your loop from taking 23 clock cycles to 22. You optimize by finding ways to reduce the O() of your algorithm. But until you run a profiler you should be paying more attention to design than any other concern.

提交回复
热议问题