Performance issue for vector::size() in a loop in C++
In the following code:
std::vector<int> var;
for (int i = 0; i < var.size(); i++);
Is the size()
member function called for each loop iteration, or only once?
In theory, it is called each time, since a for loop:
for(initialization; condition; increment)
body;
is expanded to something like
{
initialization;
while(condition)
{
body;
increment;
}
}
(notice the curly braces, because initialization is already in an inner scope)
In practice, if the compiler understands that a piece of your condition is invariant through all the duration of the loop and it does not have side-effects, it can be smart enough to move it out. This is routinely done with strlen
and things like that (that the compiler knows well) in loops where its argument isn't written.
However it must be noted that this last condition isn't always trivial to prove; in general, it's easy if the container is local to the function and is never passed to external functions; if the container is not local (e.g. it's passed by reference - even if it's const
) and the loop body contains calls to other functions, the compiler often has to assume that such functions may alter it, thus blocking the hoisting of the length calculation.
Doing that optimization by hand is worthy if you know that a part of your condition is "expensive" to evaluate (and such condition usually isn't, since it usually boils down to a pointer subtraction, which is almost surely inlined).
Edit: as others said, in general with containers it's better to use iterators, but for vector
s it's not so important, because random access to elements via operator[]
is guaranteed to be O(1); actually with vectors it usually is a pointer sum (vector base+index) and dereference vs the pointer increment (preceding element+1) and dereference of iterators. Since the target address is still the same, I don't think that you can gain something from iterators in terms of cache locality (and even if so, if you're not walking big arrays in tight loops you shouldn't even notice such kind of improvements).
For lists and other containers, instead, using iterators instead of random access can be really important, since using random access may mean walk every time the list, while incrementing an iterator is just a pointer dereference.
It's 'called' each time, but I put called into quotes because it really probably is just an inline method call, so you don't have to worry about its performance.
Why not use vector<int>::iterator
instead?
The size()
member function is called each time, but it would be a really bad implementation that wouldn't inline it, and a strange one where it wouldn't be a simple access of a fixed datum or a subtraction of two pointers.
Anyway, you shouldn't worry yourself with such trivialities until you have profiled your application and found out that this is a bottleneck.
However, what you should pay attention to is:
- The correct type for a vector's index is
std::vector<T>::size_type
. - There are types (some iterators, for example) where
i++
might be slower than++i
.
Therefore, the loop should be:
for(vector<int>::size_type i=0; i<var.size(); ++i)
...
It must be called everytime because size() might return a different value everytime.
Therefore there's no big choice it simply must be.
The problem with your question is that it does not make any sense. A C++ compiler translates some source code into a binary program. The requirement is that the resulting program must preserve observable effects of the code according to the rules of the C++ Standard. This code:
for (int i = 0; i < var.size(); i++);
simply does not have any observable effect. Moreover, it does not interact with the surrounding code any way, and the compiler may optimize it completely away; that is to generate no corresponding assembly.
To make your question meaningful, you need to specify what happens inside the loop. The problem with
for (int i = 0; i < var.size(); i++) { ... }
is that the answer very much depends on what ...
actually is. I believe @MatteoItalia provided a very nice answer, just would add a description of some experiments I made. Consider the following code:
int g(std::vector<int>&, size_t);
int f(std::vector<int>& v) {
int res = 0;
for (size_t i = 0; i < v.size(); i++)
res += g(v, i);
return res;
}
First, even if calling var.size()
will almost 100% sure be inlined with enabled optimizations, and this inlining typically translates into a subtraction of two pointers, this still brings into the loop some overhead. If a compiler is not able to prove that the vector size is preserved (which, generally, is very difficult or even infeasible, such as in our case), then you will end up with unnecessary load and sub (and, possibly, shift) instructions. The generated assembly of the loop with GCC 9.2, -O3
, and x64 was:
.L3:
mov rsi, rbx
mov rdi, rbp
add rbx, 1
call g(std::vector<int, std::allocator<int> >&, unsigned long)
add r12d, eax
mov rax, QWORD PTR [rbp+8] // loads a pointer
sub rax, QWORD PTR [rbp+0] // subtracts another poniter
sar rax, 2 // result * sizeof(int) => size()
cmp rbx, rax
jb .L3
If we rewrite the code as follows:
int g(std::vector<int>&, size_t);
int f(std::vector<int>& v) {
int res = 0;
for (size_t i = 0, e = v.size(); i < e; i++)
res += g(v, i);
return res;
}
then, the generated assembly is simpler (and, therefore, faster):
.L3:
mov rsi, rbx
mov rdi, r13
add rbx, 1
call g(std::vector<int, std::allocator<int> >&, unsigned long)
add r12d, eax
cmp rbx, rbp
jne .L3
The value of the vector's size is simply kept in a register (rbp
).
I even tried a different version where the vector is marked as being const
:
int g(const std::vector<int>&, size_t);
int f(const std::vector<int>& v) {
int res = 0;
for (size_t i = 0; i < v.size(); i++)
res += g(v, i);
return res;
}
Surprisingly, even when v.size()
cannot change here, the generated assembly was the same as in the first case (with additional mov
, sub
, and sar
instructions).
Live demo is here.
Additionally, when I changed the loop into:
for (size_t i = 0; i < v.size(); i++)
res += v[i];
then, there was no evaluation of v.size()
(subtraction of pointers) within the loop on an assembly level. GCC was able to "see" here, that the body of the loop does not alter the size any way.