Home     My Faqts     Contributors     About     Help    

faqts : Computers : Programming : Languages : Python

FAQTs repaired & updated!
Thanks for your patience...
Entry Add Entry Alert - Edit this Entry

Did You Find This Entry Useful?

89 of 94 people (95%) answered Yes
Recently 10 of 10 people (100%) answered Yes

How do Python and Java compare?

How do Python and Java compare?

Sep 29th, 2002 08:47

Magnus Lyckå, Ken Gengler, unknown unknown, Michael Chermside


Topic 1:
I would just like to know how serious python is taken in the IT industry
and how it compares to something like Java.
Answer(s):
- Recent events have proven that the computer industry is a lot less
staid and unmoving than it used to be. Everybody's getting paranoid that
someone else will beat them to the next big thing. That being so, I
expect a big cloud of Python hype any day now. Perl had it's hype day,
as did Java.
- Larger companies probably do not take Python seriously. In the
industry, though, small, faster moving companies can have an advantage.
I work at a mid-sized manufacturing plant, and we're given the freedom
to use whatever we like, as long as the plant keeps working. :) We use
OpenBSD servers running MySQL and Samba. Shell, Perl, and Python
scripting drives everything, and the Windows users are connecting using
TCL/TK apps. We do lots of things that many plants our size do not do,
or spend a lot of money doing.
That being said, it's been my experience that Python is easier to use
than Java. While I use Perl for small text parsing scripts, I've found
Python to be very well suited for creating even semi-large programs.
Like many, I was excited about Java a few years ago, but found it to be
a bit cumbersome for the small to medium sized apps that we needed to
create. I especially don't like the I/O in Java.
- Larger companies like Intel, Motorola, Microsoft, Compaq, IBM, ...?
See, for example,
<URL:http://developer.intel.com/technology/efi/toolkit_overview.htm>.
That's a cheap shot on my part.  Mr. Booth is right.  Conservative
(in some sense) MIS departments *are* wont to scorn Python.  It can
be tough getting approval from many of them to use Python for a
project such as that under consideration here.
It's worth it, though--*particularly*, I claim, for Web work.
- In the company I work for, the two languages are both used for
different (but overlapping) areas.  Java is used for the product; Python
is used for scripting tasks (automating builds, etc).
This strikes me as quite sensible.  The real reason is probably
historical, but I feel Java is better for large projects because:
+ near-static typing helps enforce interfaces
+ various language constructions (eg Interfaces) help enforce a modular
approach
+ easier to hire for
+ programmers used to Java code, which tends to be more explicit (long
winded and tedious if you like), and so easier to understand
On the other hand, Python is better for scripts because:
+ compact
+ flexible (can use functions as well as objects; objects blend nicely
  into the general synax using __ methods)
+ quicker to write
+ easier to modify
Note that the argument above doesn't mean Java is better - in many ways
it is worse, but safer/more common.  Cue arguments about blunt knives
;-)
Performance is not critical in the software I am involved in - provided
the speed is reasonable, reliability is more important.  Otherwise, I
guess we'd use C.
Topic 2:
If a web application was written in both Java and Python,
which would companies prefer?
Answer(s):
- Java. This is because IT professionals pick products that they can't
get fired for picking. An old setting in the IT industry: "nobody ever
got fired for picking Oracle."
- I heard it as "nobody ever got fired for buying IBMs".  Then the clone
industry sort of stole IBM's thunder, technological lead, etcetera.
IBMs became Also Rans. So will Java.  Once the hype evaporates, you have
just one more language, with good bits and not so good bits.
- *companies* would always prefer Java, because that has all the buzz,
they've heard of it, it's hyped all over the place. Whether they
*should* prefer Java is another question.
- I work in the IT organization of a large multinational bank (in the
top ten world-wide).
When we had to build an interface system that required mapping data
and we didn't know the target map (it kept changing - big surprise,
right?), we used Python for the mapping.  What used to take days to
change (in C++) then took at most an hour; testing became the long leg
of development....
The only problem I had in getting Python accepted was that most people
have Java experience but not Python experience.  I handled that by
having four people in the group learn Python enough to modify our
production app - it took each of them less than three days to get up
to speed.  We brought in a consultant to port the application to a new
version of the underlying library (which is SWIG wrapped) and it took
him two days.  Case made, no more problems....
BTW, we've used JPython in a second production application already.
- 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.
Topic 3:
Which is more maintainable?
Answer(s):
- In my humble opinion, I would put it this way:
    1. It takes less python code to do something in Java
    2. The Less Code you write, the less there is to maintain.
As a matter of opinion, I find Python far more "readable". The syntax is
clear, the built in data structures and types are simple enough to
grasp, but powerful enough to be useable.
- Depends on the code you write, I imagine. Python may be considered
less maintainable as it's harder to enforce interfaces, can't do static
type checking, etc. Python may be considered more maintainable as its
code is shorter, more readable, you get flatter inheritance hierarchies,
you can more easily change things around, and you can more easily
reimplement things. I myself would prefer Python; I think a solidly
engineered piece of Python could would be as maintainable if not more
than a solidly engineered piece of Java code, and faster to set up to
boot.
- Can't choose.  Python is easier to change but IMHO works better as a
prototyping and quick-mod layer, not quite as robust as Java.
- 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.
- Fewer bugs? That's arguable. If the next person that uses your API
passes an object that doesn't understand '+', you don't find out until
run time. Java would tell you at compile time. BTW, using interfaces,
you can achieve this functionality in Java as well (polymorphism).
Topic 4:
Which is more scaleable?
Answer(s):
- Keep in mind that a lot of your python can connect to third party
tools at native speeds, if you learn a trick or two.
- Scalable up or scalable down? Python scales DOWN much better. There's
a lot more overhead in making and distributing a Java application, so
Java scales down poorly.
On the other hand, I'd say more work has gone into Java, and using Java
for what would traditionally be called a "transaction processing" (TP)
system. Java has some features in it's Enterprise Java Beans
architecture specifically designed to handle high volume transaction
technology. This is not garden variety stuff, and not something that
most small or medium sized businesses need to worry about. This is more
like "I run a bank and I have 100,000 online customers accessing their
account data and doing transactions at the same time" type stuff.  Not
everybody needs Java or Enterprise Java Beans, but some people do. For
the time being at least, Python has no equivalent technology.
- As for features, I'd say this would be Java; it gets more development
in this department, and I imagine it can run on SMP machines without
any global interpreter lock.
That said, the Python environment is more dynamic which may help
you in scaling (distributed objects and so on), the Python environment
is more compact, Python may be even more portable than Java, so you
can scale up to bigger machines.
- Solely depends upon your architecture, not your language IMHO.
- 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:
:~% 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)
>>>
:~% cd app/IBMJPython-1.1
:~/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.
Topic 5:
Which is faster eg. Java servlets or Python CGI with mod_python?
Answer(s):
- Don't know about that - but there is some strong evidence that Zope
(python based application server) is faster than servelets:
http://www.zope.org/Members/BwanaZulia/zope_benchmarks/tomcat1.html
I think any major web project should consider Zope.  It has
transformaed the way I think of web development.  FWIW, I work at a
moderate sized telco and we do all of our web sites in Zope.
More on Zope at: http://www.zope.org
- In theory Java code should be faster; it does a lot more optimizing.
In practice however..
Zope seems to outspeed some Java servlet servers:
http://www.zope.org/Members/BwanaZulia/zope_benchmarks/benchmarks.html
More on Zope: http://www.zope.org
Zope is based on Python. Definitely do look at Zope if you're into
web programming.
Doing the same task in Python may often be faster than Java, possibly
due to Python's high-level constructs and the huge overhead of the
average Java runtime environment. Here's some research on that and more:
http://wwwipd.ira.uka.de/~prechelt/Biblio/jccpprtTR.pdf
Note that the *development speed* with Python is generally estimated to
be quite a bit faster than the development speed with Java. This is also
an important factor!
- Can't tell you.  However, we did some timings on Python, JPython and
Java (I rewrote a few Python/JPython classes in Java - took < 1 hour)
and found that Java was faster than both, with Python coming in next
and JPython being pretty slow.
- 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.
Final Notes:
- Well if it's performance you want, ASP and embedded COM is
probably what you want.
- Performance at the price of portability, definitely.  ASP + native COM
interfaces in C/C++ and Com+'s transactions. The "MTS" feature of COM+
would give you speed and scalability and  absolutely NO portability!
You'd also be working in an environment with a lot of inertia to
overcome whenever changes are required in the system. Python is great
from Prototype to working systems precisely because it adapts quickly.
I'd accept lower performance, use SOAP instead of COM+/MTS From what
I've read and seen, Microsoft's MTS "object runtime environment" is
extremely fast.  Writing COM components in Visual C++ can be a pain in
the butt, but if all you really need is performance, and you don't need
flexibility, it's definitely going to scale up nicely.
Now if you factor the Real and Messy world in, where requirements are
constantly changing, you can color me Python again.  I would rather
maintain and develop a system in python where I can adapt quickly to
changing requirements than work on a system where draining every last
drop of performance from the bare iron was a driving force in the design
of the system.
In the end, it's not always about performance.
- The main advantage of Python would be its readability and high
development speed. Another big advantage of Python is the existence of
Zope. Yet another advantage is Python and Zope's open sourceness. The
main advantage of Java would be its massive industry support.
- Have you seen
<URL:http://www-4.ibm.com/software/developer/library/script-survey/>?
Which is faster?  Both are.  Run-time performance of the common
implementations (and Web embeddings) of Java and Python are suffici-
ently close that the comparison depends strongly on the details of
the application under consideration.  Vulgar recognition of this
often appears as, "benchmarks are garbage."  In fact, benchmarks are
very valuable.  It's quite likely in your case that run-time perfor-
mance is *not* a significant differentiator.
Others have already written you about scalability and maintainability.
Python's more portable than Java.  You didn't ask, but you should
know that.  Java's improving, and someday will probably dominate here.
A few years ago, we thought surely it'd happen by now.  It hasn't, yet.
As others have hinted, it's not necessary to put the two in opposition.
It can be quite rational to use both Python and Java, sometimes
together, with JPython (or even more esoteric bindings).  Yes, I
understand MISthink that claims to want to standardize on One True
Language.  If that's truly a constraint on you, we can discuss
strategies for dealing with it.
- 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.
- 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.
- Coming from the Smalltalk world (which, I'd argue has all python's
good point as well as maturity), I didn't like Java at first. But, as
its matured, I've grow to love it. Java's class library is finally
mature enough to match Smalltalk's (mostly because Sun hired so many of
Smalltalk's developers). And, I'd argue that the class library makes
Java code more readable, more maintainable, less bug-ridden, and faster
to develop. Granted, late binding languages like Python and Smalltalk
will also provide fast development times. But there is a run-time cost
in terms of errors. Further, industry support for Java makes your Java
apps more easily extendible. Major DB vendors, app server vender,
transaction server vendors, graphics packages, etc., will always come
out with a Java interface before python (market reality). Moreover, the
vendor will be supporting the interface - not an individual who built a
python/php/perl interface over the weekend. I agree with an earlier
comment that python's great for small to medium size scripting-style
apps. But Java is my choice from that point on.
- If you use the Java implementation of Python--Jython--you will get the
benefits of access to all the Java libraries, Sun's as well as third
party libraries. With tools as SWIG and Boost you can fairly easily make
wrappers for C and C++ APIs, so you have the same level of third party
support as a C or C++ programmer if you stick with C based Python.
(Writing such wrappers is nothing compared with coding full applications
in C or C++.) As noticed, the dynamic nature of Python has a cost in
that some programmer mistakes that Java, C, C++ etc would catch in
compilation will appear in runtime. There are several solutions to that
problem. A good coding style and new Python features such as slots will
prevent some of the problems. Tools like pyChecker also helps. And above
all, there is no excuse for not writing thorough regression tests. You
might need to test a little more with python, but the difference is
typically small. The gain from increased productivity with python, and
from the fact that python is also excellent for writing tests, outweighs
the disadvantages by far in my opinion.



© 1999-2004 Synop Pty Ltd