Is Python really slow?

Johann Hibschman johann at physics.berkeley.edu
Sat May 27 16:02:34 EDT 2000


Alexander  writes:

> Actually I've created array and did something like this :

> for k in xrange(1,10000):
>   for m in range(1,10000):
>     x[m] =  .... #some calculation of the value of an array's element x[m]

> and this work's realy slow compared to C. I don't mind it working 3 times
> slower, but ..

Ouch.  That's precisely the kind of thing you shouldn't be doing in an
interpreted language.

First off, are these numeric arrays?  If so, have you looked at
Numeric Python?  It implements fast array arithmetic and several clever
ways to avoid explicitly writing those for-loops.

Otherwise, you might have to go with an extension module.  I've been
using python for numeric work for years now, and it's fast enough for
me.  Most of the time, the Numeric module works well enough, but
occasionally I have to move some processing to an extension.  SWIG
makes that trivially easy, though.

In general, try to avoid the giant for-loops.  For-loops are death.
If this comes up a lot, you might want to consider switching to
something like Common Lisp for speed, at least until the types-sig
gets their optimizing compiler going.  ;-)

--Johann

-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list