Hello John,

I think it would be appreciated to write your messages in the mailing list rather than contacting people personally. Using the mailing list enables sharing with everyone too.

2011/4/27 John Chamberlain <jchamberlain@cra.com>

Hi Pierre, I see from the Jython mailing list you are a Jython user and expert so I thought you might be able to help me with a Jython performance problem I am having.


I recently tried using Jython inside a Java application to do scripting and have experienced long execution times when calling eval. For example, if I call this code 10,000 times:


      PythonInterpreter mInterpreter = new PythonInterpreter();

      long t1 = System.currentTimeMillis();

      PyObject pyobject = mInterpreter.eval( "1" );

      long t2 = System.currentTimeMillis();

      if( t2 - t1 > 5 ){

            System.out.println( "eval " + (t2-t1) + " " + sExpression );



Out of 10,000 times, I get the following:


      105-115ms  5 times

      15-16ms    400 times

      0ms        all other times


This is a real problem for me because it means to populate a 100x100 array with the value 1 takes about 4-5 seconds which is not acceptable performance.



Why do you keep compiling the same thing all the time? One time is not enough? eval is very time consuming. Just reuse the result.

You can also use compile method. Here a little example in Jython to inspire you:

a = 10
b = 20
from org.python.util import PythonInterpreter
inter = PythonInterpreter()
c = inter.compile("a + b")
import inspect
print c.call(inspect.currentframe())

result: 30

The call method is very flexible. See the doc http://www.jython.org/javadoc/

Is it helping?


My blog and profile (http://pierrethibault.posterous.com)
YouTube page (http://www.youtube.com/user/tubetib)
Twitter (http://twitter.com/pierreth2)