The C++11 standard (§ 23.2.1) mandates that end
has O(1) complexity, so a conforming implementation would have the same performance characteristics for both versions.
That said, unless the compiler can prove that the return value of end
will never change then pulling end
out of the loop might be faster by some constant quantity (as Steve Jessop comments, there are lots of variables that can influence whether this is true or not).
Still, even if in one particular case there is absolutely no performance difference, pulling such tests out of the loop is a good habit to get into. An even better habit to get into is to utilize standard algorithms as @pmr says, which sidesteps the issue entirely.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…