1

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)

user1307957
  • 541
  • 3
  • 8
  • 19

1 Answers1

3

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.

nd.
  • 8,699
  • 2
  • 32
  • 42
  • Thanks for explanation. Currenly my code is little (most of work is doing library - htmlunit). I saw there's little lag when I start my code (e.g. when I add print "Starting" to beginning of my code and launch script I see output 3-4 seconds after launch), according to you this is when Jython compiles my application. I want to launch this script often (from another C++ application) so I want to avoid that 3-4 seconds lag at start. – user1307957 Jul 09 '12 at 12:55
  • 1
    @user1307957 The initial delay could be initialization logic (e.g. connecting to a database) that is slow either way. It could be Jython compiling the Python code into JVM code. It could also be the VM compiling the JVM code into machine code. Unless you measure the reason, there is no way you can tell. However, you could make a time/space tradeoff and let your program run in the background, and control if from C++ through an IPC mechanism making initialization a less frequent task. – nd. Jul 09 '12 at 13:49
  • Thanks again. One another questition is there better way how to use Java library from C++ ? – user1307957 Jul 09 '12 at 14:07
  • 1
    @user1307957 for embedding Java into C++ (=one process for everythig) look at http://stackoverflow.com/questions/7506329/embed-java-into-a-c-application etc. For C++/Java IPC (=one C++ process, one Java process) look at http://stackoverflow.com/questions/6175209/low-latency-ipc-between-c-and-java – nd. Jul 09 '12 at 14:11