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 #184930 is a reply to message #184918] Sun, 16 February 2014 20:49 Go to previous message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma:
Member
Jason C <jwcarlton(at)gmail(dot)com> writes:

> I know that I'm talking about microseconds here, so this is really
> just for the sake of furthering my own education.
>
> 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.
>
> 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);
> }

In a simple test, it seems to be a faster -- significantly faster in
relative terms but not much in absolute time. The exact numbers depend
on the length of the strings, whether one of the 100,000 strings really
is present or not, the exact keys used and so on, but in all cases that
I tested, checking strpos first pays off. A typical run: 64ms vs
33ms.

I first thought this might be because of the assignment, but it turns
out not to be. I think the cause is that str_replace has a high
overhead -- it does more before it ever gets going. If you can save
this overhead on 99,999 occasions, you save overall despite scanning the
string twice that one time.

Of course, as with all measurements like this, I may not have properly
duplicated your situation, and the result will depend on the machine,
the PHP version, and more besides, so take it with a pinch of salt.
(And note that if the key is numeric, strpos does not do what you
think!)

<snip>
> Or, would strtr() be faster? I've never actually used this function in
> practice; I think it's a slow function, but have never tested it, and
> don't remember where I read that:
>
> $firstname = strtr($firstname, $userArr);
> $lastname = strtr($lastname, $userArr);

In my test, about 89ms -- slower than either of the two above but if
it gives the right result (there is a slight difference in meaning as
pointed out elsethread) I'd probably use it for the clarity it brings.

> Any other suggestions on what might be the fastest option?

If you already have (or could have) the keys and the values in separate
arrays, using the array version of str_replace tested reasonably well
(66ms). However, as you start off by saying, it's all very marginal. I
would not normally measure this sort of thing; I'd just write the
simplest code and forget about it.

--
Ben.
[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 Oct 18 14:22:16 GMT 2024

Total time taken to generate the page: 0.06456 seconds