Game engine micro-optimization situation: I'm using C++11 range for loop to iterate over a vector<int>
, with the auto
keyword.
What is faster:
for(auto value : ints) ...
or
for(auto& value : ints) ...
?
Before caring about which is faster, you should care about which is semantically correct. If you do not need to alter the element being iterated, you should choose the first version. Otherwise, you should choose the second version.
Sure, you could object that even if you do not need to alter the content of the vector, there is still the option to use a reference to const
:
for(auto const& value : ints)
And then the question becomes: Which is faster? By reference to const
or by value?
Well, again, you should first consider whether the above is semantically correct at all, and that depends on what you are doing inside the for
loop:
int i = 0;
for (auto const& x : v) // Is this correct? Depends! (see the loop body)
{
v[i] = 42 + (++i);
std::cout << x;
}
This said, for fundamental types I would go with for (auto i : x)
whenever this is semantically correct.
I do not expect performance to be worse (rather, I expect it to be better), but as always when it comes to performance, the only meaningful way to back up your assumptions is to measure, measure, and measure.