Of the two following for loop implementations which should run faster or are they equal?
This one:
for(int i=0; i<objectPtr->bound; i++){
//do something that has no affect on objectPtr->bound
}
Or this one:
int myBound = objectPtr->bound;
for(int i=0; i<myBound; i++){
//do something that has no effect on objectPtr->bound or myBound
}
My hunch is the latter is faster but there could be some part of the compilation process that I don't understand that makes them equal in speed. I think the former will have to do an address resolution each loop cycle to determine if the value has changed.
Is there some c++ syntax that can be used to let the compiler know that the value/bound will not change. I know volatile lets the compiler know that the value will always have the possibility of changing so could I use const int for the objectPtr->bound variable to get the compiler to not always check its value each loop iteration?