Re: strpos() before str_replace()? Or, maybe strtr()? [message #184943 is a reply to message #184934] |
Tue, 18 February 2014 00:33 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>> Ben Bacarisse wrote:
>>> "J.O. Aho" <user(at)example(dot)net> writes:
>>>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>>> > Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>>>> >> Jason C wrote:
>>>> >>> Would it be faster to use strpos() before each str_replace(), like
>>>> >>> so?:
>>>> >>>
>>>> >>> foreach ($userArr as $key => $val) {
>>>> >>> if (strpos($firstname, $key) !== false)
>>>> >>> $firstname = str_replace($key, "****", $firstname);
>>>> >>>
>>>> >>> if (strpos($lastname, $key) !== false)
>>>> >>> $lastname = str_replace($key, "****", $lastname);
>>>> >>> }
>>>> >>
>>>> >> Most certainly not.
>>>> >
>>>> > And yet, when I measured it, it was faster. How can that be?
>>>> >
>>>> > <snip>
>>>>
>>>> For strpos is faster than str_replace on finding the string.
>>
>> How, and why? Both search for the occurrence of a substring in a
>> string.
>
> Because strpos has a lower per-call overhead.
That is irrelevant regarding the question which function can *find* the
string faster, is it not?
PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
|
|
|