Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Java program running 4 times faster via Eclipse than via shell?

When I execute the simple code sample below via Eclipse (version 3.5.2, on Ubuntu 10.04, java version "1.6.0_20" OpenJDK Runtime 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 java version), it takes about 40 seconds.

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

I also tried other programs, varying in runtime and amount of output: Each one was much slower in the shell. This was independent of the order of execution. The minimum percentage difference was 85 seconds in Eclipse vs. 145 seconds in shell for a program with very little output.

What's the reason?

like image 314
DaveFar Avatar asked Aug 26 '11 14:08

DaveFar


People also ask

Is Eclipse enough for Java?

The Eclipse IDE for Java Developers distribution is designed to support standard Java development. It includes support for the Maven and Gradle build system and support for the Git version control system.

Why is Eclipse good for Java?

Eclipse is one of the most popular Java IDEs on the market. It's free, open-source, and has an extensive plugin ecosystem that allows users to customize functionalities for application development.

What is debugging in Eclipse?

Debugging is the routine process of locating and removing bugs, errors or abnormalities from programs. It's a must have skill for any Java developer because it helps to find subtle bug that are not visible during code reviews or that only happens when a specific condition occurs.


2 Answers

It's because you're timing your terminal. Some terminals are just bog-slow when displaying/scrolling text. And your terminal is line buffered, vs the eclipse console likely have more buffering - leading to your program having to wait for your terminal after every line it prints.

Try redirecting the output of your program to a file or /dev/null, and time it.

On my system this makes a bit difference with your little loop:

$ 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
like image 83
nos Avatar answered Oct 24 '22 05:10

nos


Since by far the most time your program spends in doing output, the overall time of execution is very much depending on the time your system call takes for that. So putting it on the regular console seems to be much slower than the output window in eclipse, but that does not mean, your program itself is executed faster.

Just direct all output into a file and you won't see much difference any more.

like image 40
Alexander Rühl Avatar answered Oct 24 '22 05:10

Alexander Rühl