Is it expensive to compute vector size in for loops, each iteration?

后端 未结 6 774
梦毁少年i
梦毁少年i 2021-02-19 01:20

Does the c++ compiler take care of cases like, buildings is vector:

for (int i = 0; i < buildings.size(); i++) {}

that is, does it notice if

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 01:49

    buildings.size() will likely be inlined by the compiler to directly access the private size field on the vector class. So you shouldn't separate the call to size. This kind of micro-optimization is something you don't want to worry about anyway (unless you're in some really tight loop identified as a bottleneck by profiling).

提交回复
热议问题