JIT compiler and inner loop

蹲街弑〆低调 提交于 2019-12-13 20:06:26

问题


Why is the JIT-compiler not working for the following code?

And every individual loop seem to take too long time

(see my other post about executing time. Speed of simple loop

 public static void main (String[] args) {  
    for (int j = 0; j < 10; j++) {
        float f;

        long start = System.nanoTime();

        for (int i = 0; i < 2000000000; i++) {
            f = i * 0.0001F;
        }
        long end = System.nanoTime();
        long timeToCallNanoTime = System.nanoTime() - end;
        long time = Math.max(0, end - start - timeToCallNanoTime);
        System.out.println("time: " + time + " ns.");
    }

}

RESULT:

 time: 6639317628 ns.
 time: 6630196045 ns.
 time: 6632583856 ns.
 time: 6617596798 ns.
 time: 6605243858 ns.
 time: 6609097755 ns.
 time: 6627151876 ns.
 time: 6623427381 ns.
 time: 6632506712 ns.
 time: 6615870257 ns.

回答1:


It works for me in as you can see in my answer to your previous question.

Most likely you are using the client JVM which comes with 32-bit Windows. The client JVM doesn't optimise the code as much to minimise startup time. I suggest you use the 64-bit JVM which uses the -server JVM by default and it will optimise the code more aggressively.

BTW I was using Java 7 update 40. If you have a really old version of Java, it might not optimise the loop away.



来源:https://stackoverflow.com/questions/18850339/jit-compiler-and-inner-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!