Re: strpos() before str_replace()? Or, maybe strtr()? [message #184941 is a reply to message #184940] |
Mon, 17 February 2014 17:55 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 17/02/14 15:14, Ben Bacarisse wrote:
> "J.O. Aho" <user(at)example(dot)net> writes:
> <snip>
>> One of the send guys did release an pdf some year ago where he wes
>> recommending to strpos check before use of str_replace saying just
>> that the strpos was more optimized.
>
> I wonder if any cut-off point was mentioned. It must, at some point,
> start to cost more than it saves. For example, if I reverse the
> situation given by the OP so that replacements are very common rather
> than very rare, adding the strpos call nearly doubles the run time.
Don't think he mentioned, I have the document on my PowerPC machine, but
that one hasn't been turned on for a year or so. I think there was
something about using the position you got from strpos() to limit the
scope for which you use str_replace(), but a bit unsure how it was in
the example.
> Since it nearly haves it when replacements are very rare, I would not be
> surprised to find that the break even point is when about half of the
> tests will result in a replacement.
I think it was that str_replace was slow on finding the string within
and it don't stop at the first find, but has to go through the whole
string, but ultimately there will be a break even point, but in normal
situations I don't think you will be there.
> Still, the cost or benefit is likely to be insignificant for the vast
> majority of PHP programs.
I think it's the philosophy that decides, one side like microsoft who do
not believe in optimization and thinks it's just throw more hardware if
there would be issues and another extreme who always goes for optimal
optimization and of course a lot of people in between.
I think optimization should be done (to a certain level where the time
spent will not be too great), as you never will know on what your code
will be run, a smart watch with some old SoC, then you will notice the
difference while on a TOP 500 Supercomputer you will not notice it, but
better be safe than sorry.
--
//Aho
|
|
|