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 #184924 is a reply to message #184918] Sun, 16 February 2014 15:08 Go to previous messageGo to previous message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma:
Senior Member
On 02/16/2014 04:49 AM, Jason C wrote:
> 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);
> }
>
> My instinct is that it would be the same, if not marginally slower; allowing a potential computation time for 100,002 strpos() as opposed to 100,000 str_replace().
>
> 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);
>
> Any other suggestions on what might be the fastest option?
>
> TIA,
>
> Jason
>

Per http://us1.php.net/manual/en/function.strstr.php - 'strpos()' is
faster than 'strstr()' if you only want to know if a string exists in
another string.

As far as using:

> 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);
> }


It's slower because you are actually performing the same function
twice on each iteration. You are asking 'strpos()' to look for a string
and report its position (if it exists). It has to look through the
entire string to do so. Then you ask 'str_replace()' to do the same
thing only replacing the found string with a new string (if it exists).
It has to look through the entire string to do so. Since you are not
using the found position of said string ('str_replace()' can't use it)
then you are making a wasted call.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
[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:20:34 GMT 2024

Total time taken to generate the page: 0.05018 seconds