Depending on the implementation of vector, the compiler may or may not understand that size is not changed. After all, you call different vector functions inside the loop, any of which might change size.
Since vector is a template, then the compiler knows everything about it, so if it works really hard, it could understand that size doesn't change, but that's probably too much work.
Often, you would want to write like this:
for (size_t i = 0, size = vect.size(); i < size; ++i)
...
While we're at it, a similar approach is used with iterators:
for (list<int>::iterator i = lst.begin(), end = lst.end(); i != end; ++i)
...
Edit: I missed the first part:
Is this optimization?
if (x != 0) camX += x;
if (y != 0) camY += y;
if (z != 0) camZ += z;
No. First of all, even if they were int, it wouldn't be optimization since checking and branching when the values are probably most of the times not zero is more work.
Second and more importantly, they are float. This means that besides the fact that you shouldn't directly compare them to 0, they are basically almost never exactly equal to 0. So the if
s are 99.9999% true.
Same thing applies to this:
if (x != 0 | y != 0 | z != 0)
In this case however, since matrix translation could be costly, you could do:
#define EPS 1e-6 /* epsilon */
if (x > EPS || x < -EPS || y > EPS || y < -EPS || z > EPS || z < -EPS)
and now yes, comparing to a matrix multiplication, this is probably an optimization.
Note also that I used ||
which gets short-circuited if for example right from the beginning x > EPS
is true (it won't calculate the rest), but with |
that won't happen.