Why is Python so slow ?- revisited.

Bijan Parsia bparsia at email.unc.edu
Mon Jun 19 06:52:50 EDT 2000


William Dandreta <wjdandreta at worldnet.att.net> wrote:

[snip]
> The biggest improvement came (about a factor of 6) when I changed the
> replace(x,y) function in string with
> joinfields(splitfields(x,y),''). Considering that this is exactly what the
> replace function in string does, I was quite surprised at the results.
> Essentially I reduced 3 nested function calls to 2.

And that can make an enormous difference, particularly with earlier
versions. Remember that many "simple function calls" in fact involve
rather expensive polymorphic lookup and dispatching. In a tight loop
this can get very painful (Python doens't have, I believe even in the
latest versions, a method/function cache, so every lookup will go
through the whole rigamarole each time).

> The only thing that makes sense to me is that Python is spending 85% of the
> time processing the comment lines and 15% of the time doing useful work.
> There is a string.pyc so I would have assumed that it would not contain any
> comments or descriptive strings but apparently that's not true.

Well, as someone pointed out, there's a simple test for this :)

And here's another, try binding replace to a local var (outside your
loop), i.e., something like
        x=string.replace
and see if you get a comparable speedup.

> I am not very familiar with interpretive languages, Python is the first one
> I have used, but it seems that comments can cause a serious perfomance hit
> because they have to be processed along with the code.

Er...even in more straightforwardly interpreted *implementations* of
various languages, what gets interpreted is oftena  tokenized form of
the program or module which would have stripped out, or otherwise
reduced, the comments.

But, in any case, *what* Python interprets is Python byte code, not
Python source. Thus, while a compilation phase might add overhead to
your entire program, it shouldn't affect inner loops (unless you're
doing something so incredibly perverse that my poor mind couldn't handle
it ;))

-- 
Bijan Parsia
http://monkeyfist.com/
...among many things.



More information about the Python-list mailing list