通过 Java shell 执行的时间比通过控制台执行的时间更长?

发布于 2024-10-09 10:02:27 字数 956 浏览 2 评论 0原文

我有一个 Python 脚本,可以执行一些计算。当我在控制台中运行此脚本时,大约需要 7 分钟才能完成,但当我运行它时,我认为 Java shell 需要三倍的时间。我使用以下代码在 Java 中执行脚本:

this.p = Runtime.getRuntime().exec("script.py --batch", envp);

this.input = new BufferedReader(new InputStreamReader(p.getInputStream()));
this.output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
this.error = new BufferedReader(new InputStreamReader(p.getErrorStream()));

您是否有任何建议,为什么 Python 脚本在 Java 中的运行时间比在控制台中长三倍?

更新(2010年12月29日)

计算过程如下:

  1. Java将数据发送到Python。
  2. Python读取数据。
  3. Python 生成决策树——这是一个很长的操作。
  4. Python 发送树已准备就绪的确认信息。
  5. Java 收到确认。

随后Java和Python之间进行了一系列的通信,但只需要几秒钟。

更新(2010年12月29日)

感谢您的所有意见和建议。花了一个工作日才发现我的假设是错误的。我使用的代码有“错误”,实际上在控制台和 shell 中执行了不同的计算。当我修复它时,计算时间是相同的。

总结:在控制台和 Java shell 中运行的脚本的计算时间几乎相同。初始化 Java VM 和 IO 通信的额外时间是微不足道的。

I have a script in Python which do some computations. When I run this script in console it takes about 7 minutes to complete but when I run it thought Java shell it takes three times longer. I use following code to execute the script in Java:

this.p = Runtime.getRuntime().exec("script.py --batch", envp);

this.input = new BufferedReader(new InputStreamReader(p.getInputStream()));
this.output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
this.error = new BufferedReader(new InputStreamReader(p.getErrorStream()));

Do you have any suggestion why the Python script runs three time longer in Java than in a console?

update (29.12.2010)

The computation goes as follow:

  1. Java sends data to the Python.
  2. Python reads the data.
  3. Python generates a decision tree --- this is a long operation.
  4. Python sends a confirmation that the tree is ready.
  5. Java receives the confirmation.

Later there is a series of communications between Java and Python but it takes only several second.

update (29.12.2010)

Thank you for all your comments and suggestions. It took one working day to find out that my assumption was wrong. The code I used had 'a bug' and in fact different computation were performed in console and in shell. When I fixed it the computation time was the same.

Summary: The computation time of a script run in console and in Java shell is almost the same. The additional time for initialize Java VM and IO communication is insignificant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

棒棒糖 2024-10-16 10:02:27

尝试在没有 BufferedReaders 和 BufferedWriters 的情况下使用相同的代码,以防缓冲引起延迟。我不确定缓冲写入器等待刷新多长时间,但至少删除它们将有助于简化您的问题。

Try using the same code without the BufferedReaders and BufferedWriters, in case there is delay introduced by the buffering. I'm not sure how long buffered writers wait to flush, but at the very least, removing them would help simplify your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文