Skip to main content

Java efficiency


I'm playing with some piece of code calculating the time needed to compute some Java code to get a feeling of the efficiency or inefficiency of some of Java's functionality. Doing so I'm stuck now with some really strange effect I just can't explain myself. Maybe someone of you can help me understand it.




public class PerformanceCheck {

public static void main(String[] args) {
List<PerformanceCheck> removeList = new LinkedList<PerformanceCheck>();

int maxTimes = 1000000000;

for (int i=0;i<10;i++) {
long time = System.currentTimeMillis();

for (int times=0;times<maxTimes;times++) {
// PERFORMANCE CHECK BLOCK START

if (removeList.size() > 0) {
testFunc(3);
}

// PERFORMANCE CHECK BLOCK END
}

long timeNow = System.currentTimeMillis();
System.out.println("time: " + (timeNow - time));
}
}

private static boolean testFunc(int test) {
return 5 > test;
}

}



Starting this results in a relatively long computation time (remember removeList is empty, so testFunc is not even called):




time: 2328
time: 2223
...



While replacing anything of the combination of removeList.size() > 0 and testFunc(3) with anything else has better results. For example:




...
if (removeList.size() == 0) {
testFunc(3);
}
...



Results in (testFunc is called every single time):




time: 8
time: 7
time: 0
time: 0



Even calling both functions independent from each other results in the lower computation time:




...
if (removeList.size() == 0);
testFunc(3);
...



Result:




time: 6
time: 5
time: 0
time: 0
...



Only this particular combination in my initial example takes so long. This is irritating me and I'd really like to understand it. What's so special about it?



Thanks.



Addition:



Changing testFunc() in the first example




if (removeList.size() > 0) {
testFunc(times);
}



to something else, like




private static int testFunc2(int test) {
return 5*test;
}



Will result in being fast again.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. That is really surprising. The generated bytecode is identical except for the conditional, which is ifle vs ifne.

    The results are much more sensible if you turn off the JIT with -Xint. The second version is 2x slower. So it's to do with what the JIT optimization.

    I assume that it can optimize out the check in the second case but not the first (for whatever reason). Even though it means it does the work of the function, missing that conditional makes things much faster. It avoids pipeline stalls and all that.

    ReplyDelete
  2. Well, I am glad not having to deal with Java performance optimizations. I tried it myself with Java JDK 7 64-Bit. The results are arbitrary ;). It makes no difference which lists I am using or if I cache the result of size() before entering the loop. Also entirely wiping out the test function makes almost no difference (so it can't be a branch prediction hit either).
    Optimization flags improve performance but are as arbitrary.

    The only logical consequence here is that the JIT compiler sometimes is able to optimize away the statement (which is not that hard to be true), but it seems rather arbitrary. One of the many reasons why I prefer languages like C++, where the behaviour is at least deterministic, even if it is sometimes arbitrary.

    BTW in the latest Eclipse, like it always was on Windows, running this code via IDE "Run" (no debug) is 10 times slower than running it from console, so much about that...

    ReplyDelete
  3. When the runtime compiler can figure out testFunc evaluates to a constant, I believe it does not evaluate the loop, which explains the speedup.

    When the condition is removeList.size() == 0 the function testFunc(3) gets evaluated to a constant. When the condition is removeList.size() != 0 the inner code never gets evaluated so it can't be sped-up. You can modify your code as follows:

    for (int times = 0; times < maxTimes; times++) {
    testFunc(); // Removing this call makes the code slow again!
    if (removeList.size() != 0) {
    testFunc();
    }
    }

    private static boolean testFunc() {
    return testFunc(3);
    }


    When testFunc() is not initially called, the runtime compiler does not realize that testFunc() evaluates to a constant, so it cannot optimize the loop.

    Certain functions like

    private static int testFunc2(int test) {
    return 5*test;
    }


    the compiler likely tries to pre-optimize (before execution), but apparently not for the case of an parameter is passed in as an integer and evaluated in a conditional.

    Your benchmark returns times like

    time: 107
    time: 106
    time: 0
    time: 0
    ...


    suggesting that it takes 2 iterations of the outer-loop for the runtime compiler to finish optimizing. Compiling with the -server flag would probably return all 0's in the benchmark.

    ReplyDelete
  4. These benchmarks are tough since compilers are so darned smart. One guess: Since the result of testFunc() is ignored, the compiler might be completely optimizing it out. Add a counter, something like

    if (testFunc(3))
    counter++;


    And, just for thoroughness, do a System.out.println(counter) at the end.

    ReplyDelete
  5. The times are unrealistically fast per iteration. This means the JIT has detected that your code doesn't do anything and has eliminated it. Subtle changes can confuse the JIT and it can't determine the code doesn't do anything and it takes some time.

    If you change the test to do something marginally useful, the difference will disappear.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex