string object methods vs string module functions

Robin Becker robin at jessikat.fsnet.co.uk
Wed Oct 24 01:46:59 CEST 2001


In a mailing to another list Steve Alexander <steve at cat-box.net> says 

> Various modules in Zope that used to use string functions a lot have 
> recently been converted to use string methods, and there was a noticible 
> speedup in performance as measured by benchmarks / testsuites.

I would have thought this to be a minor speedup as the main work must still be in C,
but have others any thoughts/experiences on this? A function which ReportLab uses a
lot is join so my primitive hack test seems to bear out my intuition as join
performance seems very similar. Clearly I'm not actually testing this properly, but
are there any good disambiguating tests?

>>> from time import time
>>> from string import join
>>> def doit0(L,n=10000):
...     t0=time()
...     for i in xrange(n):
...             x=join(L,'')
...     print '%.2f' % (time()-t0)
... 
>>> def doit1(L,n=10000):
...     t0=time()
...     for i in xrange(n):
...             x=''.join(L)
...     print '%.2f' % (time()-t0)
... 
>>> L=[chr(i) for i in xrange(256)]
>>> doit0(L)
0.28
>>> doit1(L)
0.28
>>> 
-- 
Robin Becker



More information about the Python-list mailing list