Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12840

Re: strpos() before str_replace()? Or, maybe strtr()?

Message-ID <1816957.0LDT0NebiR@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-02-16 15:10 +0100
Subject Re: strpos() before str_replace()? Or, maybe strtr()?
Newsgroups comp.lang.php
References <cd42c387-7d7c-43de-bb0f-e12134d6ece2@googlegroups.com>

Show all headers | View raw


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.

I do not see the point of this code.  Assuming complete code, $firstname and 
$lastname are NULL in the first loop (issuing a notice each), therefore '' 
in subsequent loops (str_replace() returns that when the “haystack” is 
NULL).  Even if they were not, what sense does it make to replace parts of 
names, that match array keys, with “****”?
 
> 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.

> My instinct is that it would be the same,

Why do you think doing something twice would be as fast as doing it only 
once?

> if not marginally slower;

It would obviously be slower, already because str_replace() has to search 
for the substring before it can replace it.  Even if str_pos() somehow 
stored the position of the first occurrence of the substring, and 
str_replace() would use that, a) it would still take additional time to 
access that stored value and b) it would still be possible that there is 
more than one occurrence of the substring in the string to be searched, 
which str_pos() could not have stored.

> allowing a potential computation time for 100,002 strpos() as opposed to
> 100,000 str_replace().

Your logic is flawed: The worst case scenario is that *both* functions are 
called.
 
> 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);

It is possible that strtr() when called with a second array parameter could 
be faster than str_replace(), but by contrast to str_replace() it has no way 
of limiting the number of replacements.  Also, str_replace() supports array 
parameters both for “search” and “replace” as well.

<http://php.net/strtr>
<http://php.net/str_replace>

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

Perhaps, if I could see the point of your code.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

strpos() before str_replace()? Or, maybe strtr()? Jason C <jwcarlton@gmail.com> - 2014-02-16 01:49 -0800
  Re: strpos() before str_replace()? Or, maybe strtr()? bill <william@TechServSys.com> - 2014-02-16 07:10 -0500
  Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-16 14:55 +0100
  Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-16 15:10 +0100
    Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:24 +0000
      Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-16 21:41 +0100
        Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:51 +0000
          Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-16 22:16 +0100
            Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 23:13 +0000
              Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-18 01:33 +0100
          Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-17 06:35 +0100
            Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-17 14:14 +0000
              Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-17 18:55 +0100
            Re: strpos() before str_replace()? Or, maybe strtr()? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-17 19:30 +0100
  Re: strpos() before str_replace()? Or, maybe strtr()? Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-16 09:15 -0500
  Re: strpos() before str_replace()? Or, maybe strtr()? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-16 15:45 +0100
    Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-16 17:23 +0100
      Re: strpos() before str_replace()? Or, maybe strtr()? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-16 17:41 +0100
  Re: strpos() before str_replace()? Or, maybe strtr()? Norman Peelman <npeelman@cfl.rr.com> - 2014-02-16 10:08 -0500
    Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 19:21 +0000
      Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:53 +0000
        Re: strpos() before str_replace()? Or, maybe strtr()? Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-17 01:44 +0000
          Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-17 02:19 +0000
      Re: strpos() before str_replace()? Or, maybe strtr()? Norman Peelman <npeelman@cfl.rr.com> - 2014-02-16 18:17 -0500
  Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:49 +0000

csiph-web