Why is my Java program running 4 times faster via Eclipse than via shell?
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.746suser 0m2.403ssys 0m1.597s$ time java T >outputreal 0m5.172suser 0m2.800ssys 0m2.707s
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.
Two possibilities come to mind. First, in Eclipse, the Java machinery is already fired up; perhaps running from the shell incurs significant start-up overhead. Try timing just the loop itself (using System.currentTimeMillis()
).
Second, perhaps your configuration is such that Java running from the shell has JIT disabled. That might significantly slow down a program. Check your environment variables for anything that might disable the JIT compiler.