FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » strpos() before str_replace()? Or, maybe strtr()?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: strpos() before str_replace()? Or, maybe strtr()? [message #184942 is a reply to message #184938] Mon, 17 February 2014 18:30 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
J.O. Aho wrote:

> 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.

Um, even if it is faster now that might change in the future -- after
all this seems to be an implementation issue.

Consider how the implementation of variable interpolation has changed
within a few years.[1][2]

[1] <http://blog.golemon.com/2006/06/how-long-is-piece-of-string.html>
[2]
< http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance- Myth.html>

--
Christoph M. Becker
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: getting php mail error info
Next Topic: Declaring an array necessary?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 10:54:59 GMT 2024

Total time taken to generate the page: 0.04290 seconds