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;
}
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With