Why is Python so slow ?- revisited.

Christian Tismer tismer at appliedbiometrics.com
Sun Jun 18 20:12:41 EDT 2000


William Dandreta wrote:
> 
> Let me preface my remarks with:
> 
> --I am using the Python 1.2 binary for DOS with the Python 1.5.2 library
> which causes some problems. I avioded some problems by importing functions
> directly from strop instead of string. Replace is not in strop so I used the
> one in string but I had to comment out (put an octothorpe at the start of
> each line) the function's descriptive string to get it to work.--

Are you sure that you are talking about Python 1.2 ???
Let me assume it is 1.5.2 - ok?

> A while ago I posted a message called Why is Python so slow? At that time
> this Python script took 90 minutes to run. By making some relatively minor
> changes, I was able to reduced the time to 6 minutes.
> 
> 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.

This is a great improvement. But I see that you don't use
the most recent functionality - can it be that you missed
a couple of improvements?

1) both joinfields and splitfields are deprecated. Please use
   split and join instead.
2) Is the split(join(...)) pattern necessary here? If the pattern
   is a simple string, then you might be reasonably faster with
   a single string.replace(str, oldstr, newstr) call.

> 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.

The time spent in comment lines is very small,and it is reasonably
smaller if you use Stackless Python. Anyway, if you are concerned
about multi-line comments, then try this out:
Put a """ line in front of a longer couple of comments,
and end it the same way.
I should wonder if execution time would change reasonably.

> 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.

If you move all of your comment text into the first line of your
funcion, then it will become the DOC string, and it will cost
you not a single cycle.
Please let me know if this gives you a change of execution
speed. Also feel free to send me your source code for
inspection.

cheers - chris-the-optimizer

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com




More information about the Python-list mailing list