PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

[FIXED] Why is Java program compiled by VSCode faster than one created by manually running javac?

 November 01, 2022     java, performance, visual-studio-code, vscode-extensions     No comments   

Issue

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.


Solution

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.



Answered By - Sheng Chen
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing