简体   繁体   中英

Different compile times between IntelliJ and Eclipse IDE

My code looks like this:

public static int counter = 0;

public static void operation() {
    counter++;
}

 public static void square(int n) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            operation();
} 

public static void main(String[] args) {
    System.out.println("Start...");
    long start = System.nanoTime();

    square(350000);

    long end = System.nanoTime();
    System.out.println("Run time: " + ((end - start) / 1000000)
            + " ms");
}

I tried to run this code with IntelliJ and it took 6 500 ms, while Eclipse was much more faster with 18 ms. I'm using Skylake CPU, Java 11. Both are set on same settings and I didn't change a thing in settings.

Is there a way / How can I optimize IntelliJ to get same results as Eclipse?

Thanks.

This is not about compile time but about run time.

The Eclipse compiler and javac used by IntelliJ IDEA generate different bytecode by using different optimizations . Also on the command line, you get these different run times if you compile the Java code with the two compilers and execute it in the same Java VM.

For example, the inner loop of square(int)

    for (int j = 0; j < n; j++)
        operation();

is compiled by Eclipse to

L4
 GOTO L5
L6
 INVOKESTATIC Snippet.operation() : void
 IINC 2: j 1
L5
 ILOAD 2: j
 ILOAD 0: n
 IF_ICMPLT L6

whereas javac creates the following bytecode:

L4
 ILOAD 2: j
 ILOAD 0: n
 IF_ICMPGE L5
 INVOKESTATIC Snippet.operation() : void
 IINC 2: j 1
 GOTO L4
L5    

Semantically, both are the same, but the jump ( GOTO ) is only executed for j = 0 in the bytecode created by Eclipse, while GOTO is executed 349,999 times in the bytecode created by javac . In the combination of the machine code generated by the Java VM and the optimizations of the processor (especially inlining and branch prediction), this can lead to very different execution times as in this case (I suppose that in one case the static field counter is updated only once and in the other case it is updated 350,000 x 350,000 times).

IntelliJ IDEA is shipped with (an older version of) the Eclipse compiler which is not used by default. So using the Eclipse compiler should create the same bytecode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM