470,591 Members | 2,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Efficiency of str.replace?

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?
Jul 18 '05 #1
2 4749
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?


Consider that each call makes a new copy of the whole string. On
the other hand, whether it's a major hit or not depends entirely
on one thing: how many replacements are made. And how big the page
is -- two things. And what you consider "major" to be. There
are three things that this question depends upon, and these are....

Measurement is you only answer. Nobody else can say guess what
you would consider "major". (Not to mention what you'd consider
less elegant. I can think of some elegant, though less simple,
solutions to the potential performance problem, but I wouldn't
think for a moment of implementing them unless some profiling
proved there was a real problem.)

-Peter
Jul 18 '05 #2
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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFA2zosJd01MZaTXX0RAkTQAJwK7cBErVWrJU7zBg1tAW Q6+YDjJgCgqGA6
Ih+9xKTuAUJrFoXHA1hGdes=
=6CMv
-----END PGP SIGNATURE-----

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chris Dunaway | last post: by
4 posts views Thread by Mark P | last post: by
335 posts views Thread by extrudedaluminiu | last post: by
83 posts views Thread by Licheng Fang | last post: by
19 posts views Thread by vamshi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.