Python speed-up

Gerrit gerrit at nl.linux.org
Wed Sep 22 15:39:25 EDT 2004


Alex Martelli wrote:
> Putting together a big string with a loop of 'bigstring+=piece' is
> perhaps the best-known performance trap in Python.  Python 2.4 has
> turned somersaults to lessen the penalty you pay for this, but it's
> still pretty slow compared to "accumulate pieces in a list, ''.join the
> list when it's done".  There's really nothing better than this _in
> general_.

Why isn't cStringIO faster than concatenating strings?

Using python2.4:

$ python timeit.py -s 'from cStringIO import StringIO; s=StringIO()' "s.write('foo')"
1000000 loops, best of 3: 0.555 usec per loop

$ python timeit.py -s 's=""' "s+='foo'"
1000000 loops, best of 3: 0.383 usec per loop

$ python timeit.py -s 'L=[]' "L.append('foo')"
1000000 loops, best of 3: 0.32 usec per loop

$ python timeit.py -s 'from StringIO import StringIO; s=StringIO()' "s.write('foo')"
100000 loops, best of 3: 3.19 usec per loop

I was about to advise cStringIO as being much faster than concatenating
strings, but just before I was about to send the e-mail, I spied with my
little eye that the numbers were swapped - concatenating strings is
actually *faster* than using cStringIO! What's happening?

yours,
Gerrit Holl.

-- 
Weather in Twenthe, Netherlands 22/09 20:25:
	12.0°C   wind 5.4 m/s WSW (57 m above NAP)
-- 
Gerrit Holl - 2nd year student of Applied Physics, Twente University, NL.
Experiences with Asperger's Syndrome:
	EN http://topjaklont.student.utwente.nl/english/
	NL http://topjaklont.student.utwente.nl/



More information about the Python-list mailing list