3

I am very new to Python/Jython just started 4 weeks back.The issue with execution time for jython script which takes 14 times more compare to standalone same python script.As per my project requirements we need to integrate python/Jython script with Java application. as per Jython doc i have created JythonFacory class to call the jython script and got the script results. But when i saw the execution time (59 sec) which is a big performance issue. When i ran the same standalone python script in eclipse it was very fast just (3 sec approx).

Could you please suggest me what i should do to get the better performance. Looks like Jython is not a good option for me because of performance issue. Is there any other option to call directly the pure Python script from java without using Jython.jar

public class JythonFactory {

private static JythonFactory instance = null;

public synchronized static JythonFactory getInstance() {

    if(instance == null){

        instance = new JythonFactory();

    }

    return instance;

}

public static Object getJythonObject(String interfaceName,Map<String, Object> 

    requestTable, String pathToJythonModule) {

   Object javaInt = null;
   PythonInterpreter interpreter = new PythonInterpreter();
   interpreter.set("REQUEST_TABLE", requestTable);

   interpreter.execfile(pathToJythonModule);

   String tempName = pathToJythonModule.substring(pathToJythonModule.lastIndexOf("/")+1);
   tempName = tempName.substring(0, tempName.indexOf("."));
   //System.out.println(tempName);
   String instanceName = tempName.toLowerCase();
   String javaClassName = tempName.substring(0,1).toUpperCase() + tempName.substring(1);
   String objectDef = "=" + javaClassName + "()";
   //System.out.println("instanceName"+instanceName + " objectDef"+objectDef);
   interpreter.exec(instanceName + objectDef);
   try {
       Class JavaInterface = Class.forName(interfaceName);
       javaInt = interpreter.get(instanceName).__tojava__(JavaInterface);
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();  
    }

   return javaInt;
   }
   }
4
  • 1
    what time do you get if you run it as jython yourscript.py? Commented Jul 7, 2012 at 21:25
  • @J.F.Sebastian: It's kinda buried in the question. But he sees a minute for Jython, and 3 seconds for Cython. Commented Jul 7, 2012 at 22:38
  • 1
    @sharth: the java code indicates that jython script is not used i.e., a minute doesn't refer to jython yourscript.py (though the result might be the same). Also, Cython is not the same as CPython (Python implemented in C, the reference implementation). Commented Jul 8, 2012 at 1:56
  • @J.F.Sebastian: Those are two excellent points, and you're completely correct. Commented Jul 8, 2012 at 2:30

1 Answer 1

1

Well, I think those results are to be expected... Jython is interpreting Python code on top of Java, and I don't think it's currently optimized to use the faster method dispatch available on Java 7 (invokeDynamic): see: https://us.pycon.org/2012/schedule/presentation/446/ (and even with that, it's unclear it'd be faster than CPython).

Still, it's probably possible that you can get better performance on your program if you profile and tune it for Java (i.e.: probably optimize the needed hotspots translating those to Java -- same thing on CPython, where you'd probably translate that to C/C++).

You could try to use CPython inside Java (see: http://jepp.sourceforge.net/ ) or just invoke the Python executable directly... (not sure about your requirements thought).

Sign up to request clarification or add additional context in comments.

2 Comments

20 times difference is not expected (there is no Python code in the question so it is hard to say either way).
Thanks all of you for your valuable input. But unfortunately our java apps using 1.6_24. So for solution i have implemented one rest base webservice using python wsgiref module and where i am executing the standalone python script. Which is giving me better performance than jython script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.