where python is slower?

A. Lloyd Flanagan alloydflanagan at attbi.com
Wed Feb 5 17:40:25 EST 2003


"Enrique Palomo" <enrique.palomo at xgs-spain.com> wrote in message news:<mailman.1044449190.18425.python-list at python.org>...
> I would like to know where python is slower, reading files, writing, string
> manipulation...
> 

Python can be much slower in certain string operations, like
concatenation, because strings are immutable; every change requires a
new copy of the string.
One way around this is to use the array module.  If you create an
array from a string, you can call array.fromstring() to append
strings, then call array.tostring() when you're done.  This is very
fast.
Probably other string operations could be sped up in similar fashion.

If you've got variables like this:

somestr = "abcdefghijk"
s = array.array('c', somestr)

then:

str = somestr
for y in range(0, 10001):
    str += somestr

is much much slower than:

for y in range(0, 10001):
   s.fromstring(somestr)
str = s.tostring()

Hope this helps.




More information about the Python-list mailing list