为什么我的 Java 程序通过 Eclipse 运行的速度比通过 shell 运行的速度快 4 倍?
当我通过 Eclipse(版本 3.5.2,在 Ubuntu 10.04 上,java 版本“1.6.0_20”)执行下面的简单代码示例时 OpenJDK运行环境(IcedTea6 1.9.9)(6b20-1.9.9-0ubuntu1~10.04.2) OpenJDK Server VM(版本19.0-b09,混合模式)),大约需要10秒。当我从 shell 执行它时(使用相同的优先级和 java 版本),大约需要 40 秒。
for (int i = 0; i<1000*1000; i++) {
System.out.println(Math.cos(i));
}
我还尝试了其他程序,它们的运行时间和输出量各不相同:每个程序在 shell 中都慢得多。这与执行顺序无关。对于输出很少的程序,最小百分比差异在 Eclipse 中为 85 秒,而在 shell 中为 145 秒。
原因是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为你正在为你的终端计时。有些终端在显示/滚动文本时速度非常慢。而且你的终端是行缓冲的,而 Eclipse 控制台可能有更多的缓冲 - 导致你的程序在打印每一行后必须等待你的终端。
尝试将程序的输出重定向到文件或 /dev/null,并计时。
在我的系统上,这与你的小循环有点不同:
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:
由于到目前为止,程序大部分时间都花在输出上,因此总执行时间很大程度上取决于系统调用所花费的时间。所以把它放在常规控制台上似乎比 eclipse 中的输出窗口慢得多,但这并不意味着,你的程序本身执行得更快。
只需将所有输出定向到一个文件中,您就不会再看到太大的差异。
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.
我想到了两种可能性。首先,在 Eclipse 中,Java 机制已经启动;也许从 shell 运行会产生大量的启动开销。尝试仅对循环本身进行计时(使用 System.currentTimeMillis() )。
其次,也许您的配置是从 shell 运行的 Java 已禁用 JIT。这可能会显着减慢程序速度。检查环境变量中是否有任何可能禁用 JIT 编译器的内容。
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.
你如何测量时间?通过使用 System.nanoTime() 吗? (如果您在外部测量时间,请记住引导虚拟机的时间)。
尝试执行以下操作:
How are you measuring time? By using
System.nanoTime()
? (If you are measuring time externally, keep in mind the time to bootstrap the VM).Try to do the following:
System.nanoTime()
.