Efficiency of str.replace?

L

Leif K-Brooks

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?
 
P

Peter Hansen

Leif said:
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
 
J

Jeff Epler

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)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top