why python is slower than java?
hancock at anansispaceworks.com
Sun Nov 7 23:43:26 CET 2004
On Saturday 06 November 2004 09:07 pm, Roy Smith wrote:
> Terry Hancock <hancock at anansispaceworks.com> wrote:
> > And I have certainly written some extremely poorly optimized
> > Python programs that positively *crawled*.
> My guess is the poor performance had nothing to do with the language you
> wrote it in, and everything to do with the algorithms you used.
Wrong guess. ;-)
There was nothing wrong with the *algorithms* when considered apart
from the language. But considered *with* the language, they were
very bad choices.
Consider the case that you want to perform a string operation which
is not quite covered by the standard library. How do you solve that?
In C, you might very well write a function from scratch that does
what you want and nothing else. Usually this will be faster than
calling some standard library function that gets you halfway there
and then another that tries to fix the mistakes the first one made.
Not so in Python. Anything you write from scratch is going to be
interpreted, and therefore goes at a snail's pace compared to the
C code running in various built-ins and modules (consider the re
module, for example!). In fact, you might very well write a
regular expression solution that could have been done by less
impressive means in C, but would take much longer if you literally
translated the code from C to Python. The re solution will actually
do a lot more work "below the waterline" than the literally
translated version, but it won't have the loop overhead and such
to slow it down.
Generally speaking the simplest *conceptual* way to solve a problem
in Python is also the fastest.
I certainly learned this lesson. In a way, it's a good thing,
because the simplest conceptual way is probably the easiest for
a reader of your code to understand, too.
The reason why this is relevant is NOT because I'm saying "Python
is slow", but rather that it can be very slow if poorly programmed.
Thus an experienced Java programmer who tries Python may very well
find it to be "slow", since they will not necessarily make the
right choices in re-implementing a program in Python.
That's probably true for any two languages -- the one you are
most familiar with has a significant home-team advantage. ;-)
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
More information about the Python-list