The first version may be needlessly expensive because it relies on creating a copy of the object in the vector. Unless myType
is some very small and simple object, like an int
, storing a reference may be a better idea. It should also be declared when you need it, and no earlier, to limit aliasing issues that might otherwise cause the compiler to emit less efficient code:
vector<myType> myVec;
for(int i = 0; i < 1000000; i ++ )
{
myType& current = myVec[ i ];
doSomethingWith( current );
doAlotMoreWith( current );
messAroundWith( current );
checkSomeValuesOf( current );
}
One advantage of creating a copy, rather than using a reference, is that it might cause the compiler to load the object into a register, rather than read it from memory on every access. So both versions are worth trying.
Any of course, the copy vs reference advice applies to each of your functions too. Do they take the argument by value or reference? Depending on what they do with it, and how myType
is defined, one might be faster than the other.
The second version is flawed because it (unless the compiler is able to optimize it away) requires the object to be looked up in memory every time. Depending on your STL implementation, there may also be a bit of overhead due to bounds checking on operator[]
.
Creating a temporary first, which is then passed to each of your functions is the right way to go. The question is whether that temporary should be of value type (myType
), or reference type (myType&
/const myType&
)
Another option that may be worth exploring is putting each function call in its own separate loop. That hurts data locality in some ways, but if some of the functions use a lot of local data, it might perform better. It might also play nicer with the instruction cache.
But really, performance is extremely complicated. Caching, out of order execution, the exact semantics of myType
(especially its copy constructor and size) and the amount of optimizations performed by the compiler are all unknown to us. So we cannot give you a reliable answer.
Guess who can: your compiler. Write the test. Try both. Time the results. Pick the faster one.