0

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?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 1
    When in doubt, ask the compiler: `javap -c YourClass`. But the compiler can't assume that it can reuse the return value of `getTop()` instead of calling the method again, because it likely can't prove that the result won't change. – Andy Turner Mar 24 '17 at 21:06
  • 7
    As a general rule, prefer readability at first. Then profile code, locate hotspots which aren't performing well, and optimize for speed there. – Sam Dufel Mar 24 '17 at 21:07
  • 2
    Also, the compiler doesn't do much. It knows that JIT will be there at runtime to optimize things a lot better than it can. Don't try to "help" the compiler unless you know what you're doing. This kind of micro optimization has very little effect on the performance of a program in any case, so you're wasting precious developer time concentrating on the little things. – Kayaman Mar 24 '17 at 21:16
  • My 2c: `(int) ((1 - in) * (getSreenHeight() - getTop() - getBottom()) + getTop())` would be the clearest way to write this, under the assumption that all of these methods return the same value each time you call them. – Andy Turner Mar 24 '17 at 21:16
  • If these are just getters, the JIT will probably optimize these equivalently. – Louis Wasserman Mar 24 '17 at 21:24
  • That's why I asked the Q, so I do "know what [I'm] doing" (@Kayaman) What the JIT/compiler does/doesn't do and where I can find out. – OffGridAndy Mar 24 '17 at 22:42
  • @LouisWasserman Even if the compiler "knew" those are just getters, it won't be able to know if the returned value would or would not be changed between two consecutive calls (by another thread, for example). – Little Santi Mar 24 '17 at 23:17
  • @LittleSanti if the variable isn't `volatile` the JIT wouldn't have to notice a change in the variable from another thread anyway; it'd be allowed to do that rewrite if it could prove no code in _this_ method would change those variables. – Louis Wasserman Mar 24 '17 at 23:31
  • 1
    There are probably questions that this is a duplicate of, but it's not the question that it's actually marked as a duplicate of. – Dolda2000 Mar 25 '17 at 03:27

0 Answers0