Java vs Python

Glyph Lefkowitz glyph at twistedmatrix.com
Mon May 15 13:50:11 EDT 2000


I am a "java expert".  I've been working with the language since its
inception.  It started out as a genuinely good thing: but it has
fallen into complete decay.  If you don't decide to go with python for
this project of yours, I would highly recommend finding something
other than Java for this.  History is *already* littered with the
corpses of projects which thought that Java would solve their
problems.  (Corel Office for Java, Marimba, hundreds of unreleased,
unpublicized projects...)

I am a "python newbie".  I have been working with python for 3-6
months (I don't remember exactly how long).  Even in this short time,
I have come to love python, not because it's the end-all be-all of
programming languages, but because it actually picks some things that
it wants to be good at, and does those things very well.  The
language's strengths are well-matched to the interpreter's, and the
environment is overall a positive experience.  Not only that, I look
around every day and see successful projects, both open (Zope) and
closed (UltraSeek) that are using python with success.

Java attempts to be everything to everyone, and, as such things are
fated, becomes nothing to anyone.  Java CLAIMS to do everything well,
but actually does everything so poorly that Sun has to promise that it
will be "better in the next release", and they've been doing this for
long enough that it amazes me that people believe them anymore.

"Huy Do" <huy at nsw.bigpond.net.au> writes:

> Hi,
> 
> I would just like to know how serious python is taken in the IT
> industry and how it compares to something like Java.

"How seriously X is taken" does not necessarily relate to how good X
is.  Just like Windows NT is "taken more seriously" by many, but UNIX
is better, Java is taken more seriously, but python is better.  (I
refuse to qualify that statement: see below.)

> For example
> 
> If a web applicatoin was written in both Java and Python,
> which would companies prefer ?

Java, mostly because Java has a cooler logo, and Java is backed by
Sun, which is a big company.  Software companies, especially, prefer
this, because they like to think that having a large company behind
something means something.

That's if you're marketing it as being in a certain language.  One
thing that we do too much in this industry is focus on our
implementation technology.  YOU should be familiar with the
technology, YOU should be aware of its strengths and weaknesses, but
your customers should just know what it does and how well it does it.
It would be cool if you could get python a little bit more press, but
you don't necessarily have to try to ride Java's (or Python's)
marketing success.  If you do that, the success of your product
becomes bound up with successes and failings of the language and
platform which may or may not have anything to do with your program at
all.

> 1. Which is more maintainable ?

Python.  Python can do just about anything in fewer lines of code, and
is vastly more readable to boot (not to mention having lovely
space-saving features like function and method objects and
module-global variables.)  I don't think i've *ever* found a language
more maintainable than python, and I love it.  Even one-liners I
tossed off to solve very specific problems are completely readable
(and even somewhat reusable!) to me weeks after I write them, even if
I'm trying to be purposefully obtuse or clever ;-)

Not to mention the fact that everything is a first-class object in
python, so you don't end up worrying about how the heck you're going
to write your code to deal with int/long/double/float without
incurring extra overhead both in syntax and in memory: you can just
treat everything as a number in python.  (As if it were object
oriented or something!)

Compare the following to snippets:

class Foo:
    def __init__(self,x=15):
        self.x=x
    def bar(self, a, b):
        return self.x+a+b

...

public class Foo {
    int x;
    public Foo(int x) {
        this.x=x;
    }
    public int bar(a,b) {
        return x+a+b;
    }
}

The java version looks slightly more complex, but they appear to be
approximately the same: until you realize that Java is operating on
integers, and Python is operating on *anything which can be added*!
This could be strings, complex numbers, integers, floating point
numbers (despite the fact that we should, of course, never use
floating-point (thanks tim)). In order to get similiar functionality
in the Java version with appropriately aesthetic syntax, the code
would be about 20 times longer.  Less code == less to fix == less to
maintain == fewer bugs.

> 2. Which is more scaleable ?

Python has a global interpreter lock, so multithreading doesn't speed
it up as much.  So, if you're going to buy an Enterprise Server
1000000000 or whatever, with altogether too many processors to run
your code, the answer is *probably* java.  (There are ways around this
in python, but not all of them are immediately obvious.)  The minimum
investment to get good performance out of Java is higher than that to
get good performance out of python though.

However, Java has some issues with memory usage.  I have a machine
with more than enough RAM to do this:

glyph at helix:~% python
Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x=range(5000000)
>>> 
glyph at helix:~% cd app/IBMJPython-1.1 
glyph at helix:~/app/IBMJPython-1.1% ./jpython
JPython 1.1 on java1.3.0 (JIT: jitc)
Copyright (C) 1997-1999 Corporation for National Research Initiatives
>>> x=range(5000000)
Out of Memory
You might want to try the -mx flag to increase heap size

So it depends how you want to scale.

In JPython, which is translating the Python to Java bytecodes and
running it as it it were java, this allocation is too large.  Not
because java has more overhead and I don't have enough RAM for it, but
because Java gives up halfway through when it realizes there's not
enough space in its "allowed" memory block for that list.  Notice that
I'm using "IBMJpython" here, which is JPython installed on the newest,
funkiest JVM available for Linux, the IBM JDK 1.3 port.  It is
*possible* to increase maximum memory usage by passing commandline
options, but (A) who wants to figure out how much memory a
long-running application is going to take ahead of time and have to
restart it if it overgrows that and (B) performance begins to suffer
as you do that.

Also; you can easily bind your Python code to other systems, profile
out the performance bottlenecks, easily, by extending or embedding
Python in C or C++.

And finally; this point is often overlooked, but it is VERY important;
critical, almost -- bindings for Python to native functionality on
your platform *probably already exist*.  Java shoehorns you into ONE
API, ONE loosely-defined standard implemented shoddily by Sun, ONE way
to write GUI's, no ability to do multiplexing (all I/O is blocking and
in threads) ... all in all, the poor performance and poor scalability
of Java's standard library damn the language more thoroughly than any
other feature of it.  After all, java "scales" because it has true
multithreading that will take advantage of multiple processors, but
there are optimizing algorythms for servers that are *impossible* in
java because of library decisions (no multiplexing, as I said),
whereas Python will give them to you.

> 3  Which is faster eg. Java servlets or Python CGI with mod_python ?

Since Python does all of its I/O buffering in C, and Java does all of
its buffering in Java, Python is going to be faster, despite all of
Java's "theoretically optimal" interpreter optimizations.  If your
code is *really really* CPU bound, java might do better, but given
Java's wonky cpu-hog GC behavior, it's likely that you'll lose there
too.  (If you're seriously that CPU bound, nothing beats C, so a C
application with python "steering" would beat java anyway.)

Servlets also use the standard-output facility in java: as shown on my
Java-versus-Python page --

        http://www.twistedmatrix.com/~glyph/rant/python-vs-java.html

this is NOT very fast at all.  I have no idea why the performance
difference is so significant.

> Please do not interpret this message as an excuse to discredit java
> or python. I am about to start a major web project using Java but
> have been referred to python as an alternative so I am investigating
> this option.

I do not intend to discredit java here; it is certainly worthwhile for
some things, but it is *really* not everything Sun claims it is.
Hopefully if people realize this, Sun will actually make strides
towards delivering on all of these wonderful promises that they've
made, or relinquish control to someone who will.

The thing that Java is most useful for at the moment is interfacing
with other applications written in Java; it's very easy to link any
arbitrary java code to any other arbitrary java code, even without a
"development kit".  This is nothing on python's introspection, but
it's certainly leaps and bounds beyond C (I won't even talk about C++.
Fragile base classes?  Yuck.), and a lot of things are available to
work with.  If you can afford to take a slight speed hit for
maintainability, readability, and flexibility, but you still need
interoperability with Java, JPython is a *wonderful* thing.  Check it
out at www.jpython.org.

> Your responses are highly regarded.

> Thank you

Much luck with your web project: I hope whatever implementation
strategy you pursue is fruitful for you!

-- 
                  __________________________________________
                 |    ______      __   __  _____  _     _   |
                 |   |  ____ |      \_/   |_____] |_____|   |
                 |   |_____| |_____  |    |       |     |   |
                 |   @ t w i s t e d m a t r i x  . c o m   |
                 |   http://www.twistedmatrix.com/~glyph/   |
                 `__________________________________________'




More information about the Python-list mailing list