why python is slower than java?

Roger Binns rogerb at rogerbinns.com
Sat Nov 6 20:36:47 EST 2004


Alex Martelli wrote:
> Roger Binns <rogerb at rogerbinns.com> wrote:
>> Incidentally, that Java code copies one character at a time.
>
> Yep, I noticed -- pretty silly, but if that's how the Java designers
> decided the read method should behave by default, who am I to argue?
> Just an example I grabbed off the net, first google hit for (if I
> recall correctly) java file reading that had the source of a complete
> example.

I/O is one area where Java is *very* different than other environments.
Java emphasises the ability to select at runtime (typically from
properties files) how to do things.  This is usually done by
having layers of interfaces, and many implementations of factories
and sometimes factories of factories.  That ultimately means
the code will work the same for almost any source and destination,
including doing buffering (an interface), byte to character
conversion (an interface), the actual source (an interface),
output (another interface).

Consequently I/O is one of the hardest things for a newbie Java
programmer to do since it appears so complex due to the flexibility
offered.  It is also why bad examples exist, because people
find what works for them and post it.

Note how in Python, files do read/write whereas sockets do
send/recv.

But you are comparing Apples to Oranges.  Java programs are written
in certain ways with various emphases (flexibility, interfaces and
factories).  Python programs emphasise other things (generators,
typing).  Exceptions are expensive in Java and not used much.  They
are "cheap" in Python and used frequently.

>> The Python code is reading the entire string into memory and
>> then writing it.
>
> Right, _Python_'s default.

Arguably Python's default is reading a line at a time, and it
is a bad default in some circumstances (large files), just
as the Java code was a bad way of doing anything but small
files.

> The claim posted to this newsgroup, without any support nor examples
> being given, was that Python's I/O was far slower than Java's in
> _disk-intensive_ operations.  I'm still waiting to see any small,
> verifiable examples of that posted on this thead.

If the language code is the same, then that claim boils down to
the Java Native Interface vs the Python C API.  In the case of
Java, I can see the claim having some relevance in multi-threaded
code since Java doesn't have the GIL.

> defaults are tuned, making Python much faster.  Great, then let those
> who claim Java's I/O is much faster in disk intensive operation post
> suitable examples, and we'll see.

Your timing included the overhead of starting up and shutting down
both environments, making the rest of the measure less than
interesting.

>> Perhaps it is also worth linking to the projects done in
>> Python on SourceForge and elsewhere?
>>
>>   http://sf.net/softwaremap/trove_list.php?form_cat=178
>
> If the message you're keen to send is "Python is great for
> open-source", yes.  If you're focusing on "Python is great for your
> _business_" (as the python *business* forum does, for example), then
> emphasizing open-source projects can reasonably be considered
> secondary to emphasizing projects that make or save money for the
> businesses which developed them.

The idea isn't to emphasise the open source side, but rather so
that anyone can see for themselves how it was all put together.
If I have a business critical app, and claim it is written in
Python but noone can see the insides then they can't really
know too much.  The biggest thing is that they can't tell
if they could write code like that (or even how much was written)
to produce an app of similar functionality and complexity.

Roger






More information about the Python-list mailing list