Home > OS >  Why is Java program compiled by VSCode faster than one created by manually running javac?
Why is Java program compiled by VSCode faster than one created by manually running javac?

Time:09-23

I hava a benchmark program as follows.

public class App {

    public static void main(String[] args) {
        int n_iters = Integer.parseInt(args[0]);
        long tInit = System.currentTimeMillis();
        int c = 0;

        for (int i = 0; i < n_iters;   i) {
            for (int j = 0; j < n_iters;   j) {
                for (int k = 0; k < n_iters;   k) {
                    if (i * i   j * j == k * k) {
                          c;
                    }
                }
            }
        }

        System.out.println(c);
        System.out.println((System.currentTimeMillis() - tInit) / 1000.0);
    }
}

I put it under src directory. I compiled it by

javac src/App.java

VSCode Java extention also automatically created a working directory bin and compiled App.class under it.

Fist I ran java -cp src App 1000, it took about 1.8 seconds. Then I ran java -cp bin App 1000, this time it took 0.56 seconds.

Does it mean bin/App.class compiled by VSCode is faster than manually compiled src/App.class ? My understanding is that Java compiler does not do any optimization. JVM JIT does it. So these different runtimes don't make sense to me.

Note that there no any other source files or data files in src and bin. Other than cmd arg, the program does not depends on any external data neither.

I tested under the following environment.

  • OS: Windows 11
  • JDK: Java(TM) SE Runtime Environment (build 17.0.3.1 2-LTS-6)
  • VSCode Java Extention (redhat.java): v1.10.0

[edit]

VSCode Java extention uses ECJ compiler. https://github.com/redhat-developer/vscode-java/issues/2689

ECJ and javac compiles differently and for this task, ECJ did well. I'm still figuring out if I can get the same performance for this task with javac.

CodePudding user response:

VS Code Java use ecj to compile the Java sources, which is a different compiler implementation from javac.

I think you can use javap -verbose xxx.class to check if there is any difference between the two compiled class files.

  • Related