Which way of declaring a variable is fastest?
Posted
by ADB
on Stack Overflow
See other posts from Stack Overflow
or by ADB
Published on 2010-05-28T14:31:34Z
Indexed on
2010/05/28
14:41 UTC
Read the original article
Hit count: 131
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.
© Stack Overflow or respective owner