为什么我的 Java 程序通过 Eclipse 运行的速度比通过 shell 运行的速度快 4 倍?

发布于 2024-12-01 17:22:15 字数 454 浏览 1 评论 0原文

当我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨后彩虹 2024-12-08 17:22:15

这是因为你正在为你的终端计时。有些终端在显示/滚动文本时速度非常慢。而且你的终端是行缓冲的,而 Eclipse 控制台可能有更多的缓冲 - 导致你的程序在打印每一行后必须等待你的终端。

尝试将程序的输出重定向到文件或 /dev/null,并计时。

在我的系统上,这与你的小循环有点不同:

$ 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

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
鹿! 2024-12-08 17:22:15

由于到目前为止,程序大部分时间都花在输出上,因此总执行时间很大程度上取决于系统调用所花费的时间。所以把它放在常规控制台上似乎比 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.

醉生梦死 2024-12-08 17:22:15

我想到了两种可能性。首先,在 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.

孤芳又自赏 2024-12-08 17:22:15

你如何测量时间?通过使用 System.nanoTime() 吗? (如果您在外部测量时间,请记住引导虚拟机的时间)。

尝试执行以下操作:

  • 修改您的主要方法以首先进行热身运行而不记录时间。
  • 然后使用 System.nanoTime() 测量其他几个运行的时间。
  • 查看从控制台和 Eclipse 测量的平均时间之间是否存在显着的性能差异。

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:

  • Modify your main method to do a warm up run first without recording times.
  • Then measure the time for several others run using System.nanoTime().
  • See if there's any significant difference of performance between the averaged time measured from console and Eclipse.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文