Re: strpos() before str_replace()? Or, maybe strtr()? [message #184938 is a reply to message #184931] |
Mon, 17 February 2014 05:35 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 16/02/14 21:51, 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:
>>> <snip>
>>>> > I'm looping through roughly 100,000 values:
>>>> >
>>>> > $userArr = array(
>>>> > "something" => "anything",
>>>> > ...
>>>> > );
>>>> >
>>>> > foreach ($userArr as $key => $val) {
>>>> > $firstname = str_replace($key, "****", $firstname);
>>>> > $lastname = str_replace($key, "****", $lastname);
>>>> > }
>>>> >
>>>> > Both $firstname AND $lastname could contain any of the keys, but could
>>>> > only potentially contain one key each. It's likely, though, that neither
>>>> > will contain any of the keys.
>>> <snip>
>>>> > 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.
>
> Yes, though the question was rhetorical!
Even if your question is rhetorical:
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.
--
//Aho
|
|
|