Bjerrum
Bjerrum

Reputation: 108

Java execution from commandline is slower than in IntelliJ

I have written a simple factorial program, with arbitrary precision:

public class Fac {
    public static void main(String[] args) {
        int stop = 100000;

        long start = System.currentTimeMillis();
        BigInteger integer = new BigInteger("1");
        for(int i = 2; i <= stop; i++){
            integer = integer.multiply(new BigInteger(i +""));
        }

        System.out.println("It took: " + (System.currentTimeMillis() - start) + "ms");
        //System.out.println(integer);
    }
}

When i run it in IntelliJ:

It took: 5392ms

When i run it in commandline:

It took: 17919ms

The commandline is run by:

javac Fac.java
java Fac

I know this is not the best way to measure time but the gap is soo huge that it does not matter. Why is the performence that different?

Other people has noticed similar difference, however, as far as i can tell, their conclusions seem unrelated to my situation.

Why is my application running faster in IntelliJ compared to command-line?

http://grails.1312388.n4.nabble.com/Why-does-IntelliJ-IDEA-runs-faster-than-Windows-command-line-td3894823.html

Upvotes: 3

Views: 2315

Answers (1)

vikingsteve
vikingsteve

Reputation: 40388

It's because you are launching the jvm to run your program with different classpath, arguments, etc.

If you run the program in IntelliJ, you will see the first line of the Run window something like "C:\Program ..."

Click on it to expand it, and you will see all the arguments used when intellij runs your program (I am splitting an example over several lines here).

"C:\Program Files (x86)\Java\jdk1.8.0_40\bin\java"
-Didea.launcher.port=7532
 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\bin"
-Dfile.encoding=UTF-8
-classpath "C:\Program Files (x86)\Java\jdk1.8.0_40\jre\lib\charsets.jar;...etc..."
Fac

If you duplicate the exact same arguments (using the exact same jvm) then you will likely see similar performance when you run your application manually.

Your system settings for PATH, JAVA_HOME and CLASSPATH are used by default for launching your program if you don't specify them fully.

Upvotes: 5

Related Questions