Which way of declaring a variable is fastest?
- by ADB
For a variable used in a function that is called very often and for implementation in J2ME on a blackberry (if that changed something, can you explain)?
class X {
int i;
public void someFunc(int j) {
i = 0;
while( i < j ){
[...]
i++;
}
}
}
or
class X {
static int i;
public void someFunc(int j) {
i = 0;
while( i < j ){
[...]
i++;
}
}
}
or
class X {
public void someFunc(int j) {
int i = 0;
while( i < j ){
[...]
i++;
}
}
}
I know there is a difference how a static versus non-static class variable is accessed, but I don't know it would affect the speed. I also remember reading somewhere that in-function variables may be accessed faster, but I don't know why and where I read that.
Background on the question: some painting function in games are called excessively often and even small difference in access time can affect the overall performance when a variable is used in a largish loop.