When you're trying to optimize execution speed, loops are the most important pieces of code to look at: statements inside a loop are executed several times. Thus, even small optimizations can have a significant effect.
A standard method to speed up loops is to move constant expressions out of the loop. For example, the following simple for loop iterates all elements of the Vector vector:
for (int i = 0; i < vector.size(); i++) ...
In this line, the size() method of vector is called at each iteration. This repetition can be avoided by storing the size in a local variable:
int size = vector.size (); for (i for (int i = 0; i < size; i++) ...
Please note that access to local variables is generally less expensive than access to instance or class variables. Here, the size() method is called only once and stored in a local variable for fast access inside the loop. If the direction the vector is iterated is not important, counting down may be a reasonable alternative to introducing a new variable:
for (int i = vector.size()-1; i >= 0; i--) ...
The same technique can be applied to all expressions that are calculated inside the loop, but do not change with the iterations.