lunedì 29 novembre 1999

J2ME performance tips: improve your for/while loops

When you write a desktop or a server application you can often ignore some small tips that in a mobile application can be very powerful.

Let's talk about loops. You have to avoid as much as possible operations in the loops that you can perform once out of the loop. For example:

for ( int i = 0; i < 5; i++ ) {
   int a = 10;
   int m = 5;
   int f = i + (m * a);
}

can be written like:

int a = 10;
int m = 5;

for ( int i = 0; i < 5; i++ ) {
   int f = i + (m * a);
}


avoiding five assignments.

Another good practise is avoiding call of a method in the conditional block of the loop. If you use the size of a vector as limit of your loop you have to avoid:

for ( int i = 0; i < vector.size(); i++ ) {
   int f = i + (m * a);
}


and write instead:

int limit = vector.size();
for ( int i = 0; i < limit; i++ ) {
   int f = i + (m * a);
}


The suggestions are also explained at J2ME DevCorner blog

Nessun commento:

Posta un commento