In order to give functions the option to modify the vector I can’t do
curr = myvec.at( i );
doThis( curr );
doThat( curr );
doStuffWith( curr );
But I have to do:
doThis( myvec.at( i ) );
doThat( myvec.at( i ) );
doStuffWith( myvec.at( i ) );
(as the answers of my other question pointed out)
-
I’m going to make a hell lot of calls to
myvec.at()then. How fast is it, compared to the first example using a variable to store the result? -
Is there a different option for me? Can I somehow use pointers?
When it’s getting serious there will be thousands of calls to myvec.at() per second. So every little performance-eater is important.
You can use a reference:
The
atmember function does bounds checking to make sure the argument is within the size of thevector. Profiling is only way to know exactly how much slower it is compared tooperator[]. Using a reference here allows you to do the lookup once and then use the result in other places. And you can make it a reference-to-constif you want to protect yourself from accidentally changing the value.