问题描述
为什么JIT编译器不能使用以下代码?
Why is the JIT-compiler not working for the following code?
每个循环似乎都花费了很长时间
(请参阅我的其他有关执行时间的文章.简单循环的速度
(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.");
}
}
结果:
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.
推荐答案
它对我有效,正如您在上一个问题的答案中所见.
It works for me in as you can see in my answer to your previous question.
您很有可能正在使用32位Windows附带的客户端JVM.客户端JVM并未对代码进行尽可能多的优化以最小化启动时间.我建议您使用默认情况下使用-server
JVM的64位JVM,它将更加积极地优化代码.
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.
顺便说一句,我正在使用Java 7 update40.如果您的Java版本真的很旧,它可能无法优化循环.
BTW I was using Java 7 update 40. If you have a really old version of Java, it might not optimise the loop away.
这篇关于JIT编译器和内部循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!