The difference is going to be too chaotic to have a general rule.
The two bits of code are logically equivalent, so under as-if the compiler is free to treat one as the other.
Things change if the value or address of i
is taken, especially if it is passed "non locally", as that can force a compiler to give up the ability to transform one to the other.
Some compilers may be confused by one, but not the other.
The advantage of the first one is you are iterating over indexed elements.
The advantage of the second one is you are mimicing "modern" C++ iterator syntax. (I would, however, be tempted to use !=
rather than <
, as comparing pointers past one-past-the-end of a structure is undefined behavior, so you should be certain to stop at the end, not blow past it).
The real problem with your question is that it is almost certainly pre-mature optimization. If it is not pre-mature optimization, you should be certain that this code is a performance bottleneck, and you should be profiling and determine which is faster rather than looking for rules of thumb.
At the level of "writing code that isn't prematurely pessimized", neither really wins. The importance of clarity trumps any performance difference you will experience. Clarity is important to performance because clear, easy to understand code is easier to optimize, and directed optimization (where you take a performance bottleneck and make it perform better) is a far better use of performance improvement time than reading over unclear code caused by unneeded micro optimizations.