user1307957
user1307957

Reputation: 541

Compile application using Jython

I wrote some code in Python which is using Java libraries and interpreted it using Jython, but it's a bit slow. Can I somehow compile this code instead of interpreting it every time I launch my script ? (I read about jythonc but it's deprecated in new version of Jython)

Upvotes: 1

Views: 1470

Answers (1)

nd.
nd.

Reputation: 8932

Jython, javac and jythonc

Jython always compiles your application on launch - i.e. if you start your application, then the Python-code is compiled into Java VM bytecode; the VM then executes this bytecode. The difference between Jython and javac is that javac creates .class files containing the bytecode whereas Jython creates the bytecode at runtime.

jythonc does for Python code the same that javac does for Java code: it compiles the code to .class files and saves it to disk. Performance-wise, this is not better than Jython's standard behavior, but it makes it possible to use/extend Jython code in other JVM languages.

jythonc does not improve the performance of your program in any way.

Improving performance

Use a profiler to detect the hot spots of your code. The profiler will show you information where your program spends most of its time:

Example output of the NetBeans Profiler showing that the constructor of Anagrams took >70% of the time of the application run.

Once you know the hotspots of your application, you will know how to optimize its performance. In the screenshot above, more than 70% of the time of the application is spent in the constructor (<init>).

You then can use several techniques to improve your performance:

  1. Use a better algorithm. If applicable, this has the highest impact on performance.

  2. Trade execution time for space. Cache results of functions you will invoke often, especially if they are relatively slow - e.g. data retrieved by database or network access.

  3. Re-implement the hotspot in a language with less overhead. In your case, you could re-implement the hotspots in Java and call them from Jython.

Upvotes: 3

Related Questions