Python 2 times slower than Perl

Gareth McCaughan Gareth.McCaughan at pobox.com
Sun Jul 22 01:16:42 CEST 2001


C S Xu wrote:

> Speed issue is due to recently I helped my classmate to do a
> calculation on his study. The calc. time increases as ~ 2^n,
> where n is number of particle pairs (2 particle forms a bond
> called a pair). In python, n=15 already took me 10 minutes to
> get the result. So I am curious what improvement I can obtain
> if I change the code into C/C++. According to my previous
> experiences, NumericPy doesn't give much more speed as claimed.
> I remember it is only about 2 times faster than original python.

If the calculation time goes up like 2^n, then a factor of 2
in speed is only going to buy you one more particle pair.
A factor of 100 will get you between 6 and 7 more. Is that
enough to make a big difference? You might do better looking
for algorithmic improvements, though I appreciate that
finding such improvements might be a major research project.

With Python code that's similar to C or C++ code translated
into Python -- like your benchmark loop -- you can probably
expect a factor of 100 from moving to C or C++. For most
programs, you can get more or less the same improvement by
translating only a small amount of the code into C or C++
and leaving the rest in Python. (Gluing C and Python together
is generally very easy, especially when you're writing the
code on both sides.) But please note: often you won't know
*which* small portion of the code to do in C or C++ until
you've written it all and timed it. So start in Python,
get the program working, and then look at moving bits of it
to C for speed.

> Anyway, thanks for all comments. I've ever considered Python
> was always 2 times slower than Perl everywhere (it is true on
> my working SunOS, :-( ), now I'm corrected.

I simply do not believe that Python is twice as slow as Perl
for all programs, on *any* system. Try the ones I posted
a day or two ago.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
.sig under construc



More information about the Python-list mailing list