3 ms·
your simpler code is at least theoretically less efficient - you get an int to size_t (unsigned long long) conversion twice (once when comparing with size() and
by jcelerier 7y ago
your simpler code is at least theoretically less efficient - you get an int to size_t (unsigned long long) conversion twice (once when comparing with size() and once when accessing the vec) and you compare to the size every time. This looks like it is irrelevant in release mode, but it may end up having an effect on your debug performance which is important in many domains.
You would have to write
for(std::size_t i = 0, N = vec.size(); i < N; i++)
// do something with vec[i]
instead if you still insist in raw loops.
But if you look at https://gcc.godbolt.org/z/8JwJbw https://gcc.godbolt.org/z/8JwJbw, you will notice that the range-based loop is actually the one which will produce the smaller code which may translate into more efficiency, and is also more readable.