Loop Condition Checking

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 ...

Get Java™ 2 Micro Edition Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.