Why is my Java program running in Eclipse 4 times faster than through the shell?

When I execute a simple code example through Eclipse (version 3.5.2, on Ubuntu 10.04, java version "1.6.0_20", OpenJDK working environment (IcedTea6 1.9.9) (6b20-1.9.9-0ubuntu1 ~ 10.04.2) OpenJDK Server VM (build 19.0-b09, mixed mode)), it takes about 10 seconds. When I execute it from my shell (using the same priority and version of java), it takes about 40 seconds.

for (int i = 0; i<1000*1000; i++) { System.out.println(Math.cos(i)); } 

I also tried other programs, changing the execution time and output volume: each of them was much slower in the shell. It did not depend on the order of execution. The minimum percentage difference was 85 seconds in Eclipse versus 145 seconds in the shell for a program with very little output.

What reason?

+7
source share
4 answers

This is because you are synchronizing your terminal. When displaying / scrolling text, some terminals simply swamp. And your terminal is buffered in a line, and the eclipse console probably has more buffering - which causes your program to wait for your terminal after every line that it prints.

Try redirecting your program output to a file or / dev / null and time.

On my system, this changes your little loop a bit:

  $ time java T
  --snip - 1M lines of output--

 real 0m24.746s
 user 0m2.403s
 sys 0m1.597s

 $ time java T> output

 real 0m5.172s
 user 0m2.800s
 sys 0m2.707s
+19
source

Since in most cases your program spends time on execution, the total execution time very much depends on the time it takes for your system call. Therefore, placing it on a regular console seems to be much slower than the output window in eclipse, but this does not mean that your program runs faster.

Just direct all the output to a file and you will no longer see any difference.

+6
source

Two possibilities come to mind. Firstly, in Eclipse, the Java machine is already running; perhaps a significant overhead comes from the command shell. Try synchronizing only the loop itself (using System.currentTimeMillis() ).

Secondly, perhaps your configuration is such that Java running from the shell has JIT disabled. This can significantly slow down the program. Check your environment variables for anything the JIT compiler can disable.

+4
source

How do you measure time? Using System.nanoTime() ? (If you are measuring time from the outside, keep in mind the time to load the VM).

Try the following:

  • Change your main method to warm up first without recording time.
  • Then measure time for several others using System.nanoTime() .
  • See if there is a significant performance difference between the average time measured from the console and Eclipse.
+2
source

All Articles