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
Assuming the size()
function is an inline function for the base-template, one can also assume that it's very little overhead. It is far different from, say, strlen()
in C, which can have major overhead.
It is possible that it's still faster to use int n = buildings.size();
- because the compiler can see that n
is not changing inside the loop, so load it into a register and not indirectly fetch the vector size. But it's very marginal, and only really tight, highly optimized loops would need this treatment (and only after analyzing and finding that it's a benefit), since it's not ALWAYS that things work as well as you expect in that sort of regard.