January 29, 2015
Hot Topics:

Introduction to Jython

  • March 25, 2003
  • By Developer.com Staff
  • Send Email »
  • More Articles »

For Python Programmers

If you are already a Python programmer, you know about the programming features of Python and are probably wondering what you gain from the Java side of the equation. There seem to be at least three important gains.

Extended reach
Using Jython, you can deploy a Python language program in arenas that are simply not possible using CPython. The most notable examples of this are web-based--namely, the creation of applets that will be accepted by mainstream browsers, and the creation of servlets that can be run by Java servlet or J2EE servers.

Ease of distribution
Jython makes it easy to distribute your program as a cross-platform binary in .jar format. Although there are binary tools for CPython on different platforms, there is no similar cross-platform tool for CPython.

Tools and libraries
This is the biggest advantage of Jython. Although the Python standard library and available third-party tools are varied and wonderful, Java has benefited from a much larger pool of available tools, particularly enterprise-level tools. The Swing GUI toolkit is more complete and more cross-platform than any GUI toolkit available for CPython, and is arguably easier to use. Database tools are much more widely available and standardized in the Java world. There is a large quantity of high-level tools for XML data. All these libraries and tools can be used from Jython at least as easily as other Java tools would be able to use them.

The Speed Question

Everybody asks it, at some point. Whenever you try to sell somebody on using a high-level scripting-style language, the question will inevitably come up: "Isn't it slow?" As long-time programmers of scripting languages and other languages that have been branded as slow (including Java), we have a variety of reasons why raw benchmark speed is less important than it might seem as a factor in choosing a programming tool. In fact, we'll also argue that for most of you, most of the time, the speed difference is not going to hurt at all.

What is the exact magnitude of the speed difference? It's notoriously difficult to get good information about performance across languages. Sometimes, the person compiling the numbers has an axe to grind and has chosen a task that favors one language. More often, the person compiling the examples has just done a better job optimizing one version over the other. So, any cross-language speed comparison needs to be taken with a grain of salt.

That said, one of the authors' experience in running identical code under both Python 2.1 and Jython 2.1a3 (using Sun's JDK 1.3) suggests that CPython takes 75% of the time that Jython does. Jython is about 1-10 times slower than equivalent Java code, although admittedly useful hard numbers on this are hard to come by because the exact speed difference strongly depends on the nature of your application. Programs that spend a lot of time doing operations on primitive data types will show the largest speed difference between Java and Jython.

Jython performance is heavily dependent on the specifics of the JVM, of course. Specifically, the Sun JDK 1.4 is expected to make significant improvements in the performance of Java reflection, and as a result Jython should make substantial speed gains under JVM 1.4.

Still, up to 10 times slower seems like a big speed difference. And for some programs and some projects it might be. But for most programs and projects, the benchmark speed hit will not be as noticeable in practice as you might think. There are four reasons why the speed loss is not as harmful in practice as it is in benchmarks.

For a large number of programs, the bottleneck is either user input speed or network latency, and even a large performance hit in these cases is not noticeable to users. Java programs tend to be in this category frequently--if raw speed was the issue, you probably wouldn't be using Java in the first place.

Even for programs in which optimal performance is particularly important, profiling will often indicate that a small number of classes or methods are the bottleneck. In Jython, these areas can be targeted and rewritten in Java so that you get the maximum performance benefit for the minimum effort.

There's what we might call the Peters principle, after CPython core developer, Tim Peters. He has suggested that his Python programs often run faster than his C programs in practice, because development is so much easier in Python that you often wind up with a much better algorithm than you would in C. In particular, common performance enhancements such as caching function results are quite easy to code in Python.

Finally, as in so many aspects of software design, there's a trade off. If each run of your program takes five seconds longer, but you get it to your users three months sooner than you otherwise would have, what's that worth to you? How much value is created in the development time saved? For many projects, three months of programmer time is worth far more than the user time lost if the program is slower. For some projects, of course, the speed is still critical.

As always, the rule is "Make it work, then make it fast." Jython is a fabulous environment for making it work, and if at some point you find that you still need to make it faster, there are mechanisms to try, both within Jython and by converting some code to Java.

About the Author

Samuele Pedroni is one of the main Jython developers. He holds a CS flavored diploma in mathematics from the ETH Zurich (Swiss institute of technology in Zurich). He is now a teacher and research assistant at the Institute of Theoretical CS. He plans to come to the US for a PhD in the field of dynamic languages and dynamic compilation. He came to Jython with the interest in improving it with respect to Java importing and reloading. He has developed several important patches related to java integration, classloaders, and the reworking of java/python importing rules and design.

Noel Rappin has a Ph.D. in computer science from the Georgia Institute of Technology, where his research included methods for teaching Object-Oriented Programming and Design. He has extensive production experience in both Java and Python. Noel also contributed an introductory chapter to the book Squeak: Open Personal Computing and Multimedia (PH).

Source of this material

This is Chapter 1: Introduction to Jython from the book Jython Essentials (ISBN:0-596-00247-5) written by Samuele Pedroni and Noel Rappin, published by O'Reilly & Associates.

To access the full Table of Contents for the book

Page 4 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel