Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longer execution through Java shell than console?

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.

like image 873
czuk Avatar asked Dec 28 '10 11:12

czuk


1 Answers

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.

like image 177
James Branigan Avatar answered Oct 20 '22 14:10

James Branigan