通过 Java shell 执行的时间比通过控制台执行的时间更长?
我有一个 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日)
计算过程如下:
- Java将数据发送到Python。
- Python读取数据。
- Python 生成决策树——这是一个很长的操作。
- Python 发送树已准备就绪的确认信息。
- 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:
- Java sends data to the Python.
- Python reads the data.
- Python generates a decision tree --- this is a long operation.
- Python sends a confirmation that the tree is ready.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在没有 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.