Efficiency of str.replace?

Jeff Epler jepler at unpythonic.net
Thu Jun 24 22:31:40 CEST 2004


On Wed, Jun 23, 2004 at 09:08:43PM -0400, Leif K-Brooks wrote:
> For my web-based application, I need to make twelve different calls to 
> str.replace for the content of /every/ page on /every/ page view (or 
> find a harder to implement and probably less elegant solution). Would 
> that be a major performance hit?

$ timeit -s 's = open("/usr/share/dict/words").read()' 's.replace("the", "xyzzy")'
100 loops, best of 3: 8.31e+03 usec per loop

/usr/share/dict/words contains 45k words in 410k bytes, with "the"
appearing on 409 of those lines.  When I take the first 50000 bytes of
the file, I get a timing of 360 usec per loop.

Is 100 msec much per page view?  Is 4 msec?  What's the size of an
average page?  I can't answer any of these questions for you.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040624/6f1710bb/attachment.sig>


More information about the Python-list mailing list