JIT compiler and inner loop

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.

Upvotes: 1

Views: 126

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

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.

Upvotes: 3

Related Questions