Efficiency of str.replace?

Peter Hansen peter at engcorp.com
Wed Jun 23 22:09:23 EDT 2004


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



More information about the Python-list mailing list