The following Java code snippets are just to illustrate my question.
// Take a value (0 - 1) and
// return a screen pixel value.
public int toScreen1(float in){
int visibleHeight = getSreenHeight() - getTop() - getBottom();
float pixelOffset = visibleHeight * in;
int screenPixel = (int) (visibleHeight - pixelOffset + getTop());
return screenPixel;
}
public int toScreen2(float in){
return (int) (getSreenHeight() - getTop() - getBottom() -
((getSreenHeight() - getTop() - getBottom()) * in) +
getTop());
}
public int toScreen3(float in){
int screenHeight = getSreenHeight();
int top = getTop();
int bottom = getBottom();
int o = (int) (screenHeight - top - bottom -
((screenHeight - top - bottom) * in) +
top);
return o;
}
They all do the same thing but toScreen2
sacrifices readability for the sake of assigning some variables. Would this make much difference to speed? Or, as the variables are used only within the method, is the compiler smart enough to make these equivalent? toScreen3
purposefully assigns variables to avoid multiple method calls. Again, what speed impact or will the compiler make the best optimizations whichever method I use? Could someone point me in the direction of documentation that'll tell me what optimizations the Java compiler makes and what I should consider myself?