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


Groups > comp.lang.php > #12837 > unrolled thread

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

Started byJason C <jwcarlton@gmail.com>
First post2014-02-16 01:49 -0800
Last post2014-02-16 20:49 +0000
Articles 20 on this page of 25 — 9 participants

Back to article view | Back to comp.lang.php


Contents

  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

Page 1 of 2  [1] 2  Next page →


#12837 — strpos() before str_replace()? Or, maybe strtr()?

FromJason C <jwcarlton@gmail.com>
Date2014-02-16 01:49 -0800
Subjectstrpos() before str_replace()? Or, maybe strtr()?
Message-ID<cd42c387-7d7c-43de-bb0f-e12134d6ece2@googlegroups.com>
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

[toc] | [next] | [standalone]


#12838

Frombill <william@TechServSys.com>
Date2014-02-16 07:10 -0500
Message-ID<ldq9rd$o8u$2@speranza.aioe.org>
In reply to#12837
On 2/16/2014 4: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
>

Why would you want the opinion of a bunch of Old Farts (sorry 
Jerry) when you could easily test it yourself and instruct us ?

-bill
Vetustior Humo

[toc] | [prev] | [next] | [standalone]


#12839

From"J.O. Aho" <user@example.net>
Date2014-02-16 14:55 +0100
Message-ID<bmbuahF5k1jU1@mid.individual.net>
In reply to#12837
On 16/02/14 10:49, 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?:

Yes, if you only make str_replace() when it really need it, then the 
code will be faster and require less CPU cycles (of course the faster 
the machine, the less of the ill effects you will notice, but it will 
still be there).


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

Would value the CPU and time usage to be around the same as for 
str_replace(), they both need to find the string and then build the new one.

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#12840

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-16 15:10 +0100
Message-ID<1816957.0LDT0NebiR@PointedEars.de>
In reply to#12837
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

[toc] | [prev] | [next] | [standalone]


#12847

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-16 20:24 +0000
Message-ID<0.9da5fd4e321b89e9adc3.20140216202439GMT.877g8u7qm0.fsf@bsb.me.uk>
In reply to#12840
Thomas 'PointedEars' Lahn <PointedEars@web.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>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#12848

From"J.O. Aho" <user@example.net>
Date2014-02-16 21:41 +0100
Message-ID<bmcm4cFarddU1@mid.individual.net>
In reply to#12847
On 16/02/14 21:24, Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.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.

-- 
  //Aho

[toc] | [prev] | [next] | [standalone]


#12850

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-16 20:51 +0000
Message-ID<0.1d3b7c8128d5ae4333af.20140216205119GMT.87siri6at4.fsf@bsb.me.uk>
In reply to#12848
"J.O. Aho" <user@example.net> writes:

> On 16/02/14 21:24, Ben Bacarisse wrote:
>> Thomas 'PointedEars' Lahn <PointedEars@web.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!

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#12852

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-16 22:16 +0100
Message-ID<2051024.CPFVgIA8O1@PointedEars.de>
In reply to#12850
Ben Bacarisse wrote:

> "J.O. Aho" <user@example.net> writes:
>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.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.

How, and why?  Both search for the occurrence of a substring in a string.
 
> Yes, though the question was rhetorical!

The question was stupid.


PointeDears
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#12853

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-16 23:13 +0000
Message-ID<0.72f969f4ca3b7fcb1715.20140216231324GMT.87ha7y648b.fsf@bsb.me.uk>
In reply to#12852
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> "J.O. Aho" <user@example.net> writes:
>>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>>> Thomas 'PointedEars' Lahn <PointedEars@web.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.
>
> How, and why?  Both search for the occurrence of a substring in a
> string.

Because strpos has a lower per-call overhead.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#12862

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-18 01:33 +0100
Message-ID<32394685.JNGdiLLVUf@PointedEars.de>
In reply to#12853
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> "J.O. Aho" <user@example.net> writes:
>>>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>>>> Jason C wrote:
>>>>>>> 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.
>>
>> How, and why?  Both search for the occurrence of a substring in a
>> string.
> 
> Because strpos has a lower per-call overhead.

That is irrelevant regarding the question which function can *find* the 
string faster, is it not?


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [next] | [standalone]


#12857

From"J.O. Aho" <user@example.net>
Date2014-02-17 06:35 +0100
Message-ID<bmdldaFgpldU1@mid.individual.net>
In reply to#12850
On 16/02/14 21:51, Ben Bacarisse wrote:
> "J.O. Aho" <user@example.net> writes:
>
>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.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

[toc] | [prev] | [next] | [standalone]


#12859

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-17 14:14 +0000
Message-ID<0.ebd3223c90ed07830c4a.20140217141429GMT.87k3ct4yii.fsf@bsb.me.uk>
In reply to#12857
"J.O. Aho" <user@example.net> writes:
<snip>
> 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.

I wonder if any cut-off point was mentioned.  It must, at some point,
start to cost more than it saves.  For example, if I reverse the
situation given by the OP so that replacements are very common rather
than very rare, adding the strpos call nearly doubles the run time.
Since it nearly haves it when replacements are very rare, I would not be
surprised to find that the break even point is when about half of the
tests will result in a replacement.

Still, the cost or benefit is likely to be insignificant for the vast
majority of PHP programs.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#12860

From"J.O. Aho" <user@example.net>
Date2014-02-17 18:55 +0100
Message-ID<bmf0o1Fps1tU1@mid.individual.net>
In reply to#12859
On 17/02/14 15:14, Ben Bacarisse wrote:
> "J.O. Aho" <user@example.net> writes:
> <snip>
>> 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.
>
> I wonder if any cut-off point was mentioned.  It must, at some point,
> start to cost more than it saves.  For example, if I reverse the
> situation given by the OP so that replacements are very common rather
> than very rare, adding the strpos call nearly doubles the run time.

Don't think he mentioned, I have the document on my PowerPC machine, but 
that one hasn't been turned on for a year or so. I think there was 
something about using the position you got from strpos() to limit the 
scope for which you use str_replace(), but a bit unsure how it was in 
the example.


> Since it nearly haves it when replacements are very rare, I would not be
> surprised to find that the break even point is when about half of the
> tests will result in a replacement.

I think it was that str_replace was slow on finding the string within 
and it don't stop at the first find, but has to go through the whole 
string, but ultimately there will be a break even point, but in normal 
situations I don't think you will be there.


> Still, the cost or benefit is likely to be insignificant for the vast
> majority of PHP programs.

I think it's the philosophy that decides, one side like microsoft who do 
not believe in optimization and thinks it's just throw more hardware if 
there would be issues and another extreme who always goes for optimal 
optimization and of course a lot of people in between.

I think optimization should be done (to a certain level where the time 
spent will not be too great), as you never will know on what your code 
will be run, a smart watch with some old SoC, then you will notice the 
difference while on a TOP 500 Supercomputer you will not notice it, but 
better be safe than sorry.

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#12861

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-02-17 19:30 +0100
Message-ID<53025550$0$6662$9b4e6d93@newsspool2.arcor-online.net>
In reply to#12857
J.O. Aho wrote:

> On 16/02/14 21:51, Ben Bacarisse wrote:
>> "J.O. Aho" <user@example.net> writes:
>>
>>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>>> Thomas 'PointedEars' Lahn <PointedEars@web.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

[toc] | [prev] | [next] | [standalone]


#12841

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-02-16 09:15 -0500
Message-ID<ldqh6m$q7i$1@dont-email.me>
In reply to#12837
On 2/16/2014 4: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
>

The real question here is - what does it matter?  Do you have a 
performance problem?  If so, what's the problem (I can about guarantee 
it isn't here).

You can test it yourself, but that test is only going to be valid for 
your current situation.  Change versions of PHP, your OS or even the 
means of calling PHP (i.e. CLI vs. Apache Module) may change the 
results.  And in this particular case, it will be highly dependent on 
the data (i.e. the percentage of times your key appears in the strings).

You are much better off coding for clarity and testing.  If you have a 
performance problem, then is the time to find your performance problem 
and fixing it.  Donald Knuth had it right when he said "Premature 
optimization is the root of all evil."

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#12842

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-02-16 15:45 +0100
Message-ID<5300cef8$0$6662$9b4e6d93@newsspool2.arcor-online.net>
In reply to#12837
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?

Having the fastest option is surely less important than having a correct
solution, and there is an important semantic difference between
str_replace() and strtr().  Consider the following snippet:

  $array = array('some' => 'any', 'anything' => 'nothing');
  $name = 'something';

  $keys = array_keys($array);
  $values = array_values($array);
  var_dump(str_replace($keys, $values, $name));

  var_dump(strtr($name, $array));

This will output:

  string(7) "nothing"
  string(8) "anything"

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#12844

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-02-16 17:23 +0100
Message-ID<2411212.LVumIkLPpi@PointedEars.de>
In reply to#12842
Christoph Michael Becker wrote:

> Jason C wrote:
>> 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);
>> }
>> 
>> […]
>> 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?
> 
> Having the fastest option is surely less important than having a correct
> solution, and there is an important semantic difference between
> str_replace() and strtr().  Consider the following snippet:
> 
>   $array = array('some' => 'any', 'anything' => 'nothing');
>   $name = 'something';
> 
>   $keys = array_keys($array);
>   $values = array_values($array);
>   var_dump(str_replace($keys, $values, $name));
> 
>   var_dump(strtr($name, $array));
> 
> This will output:
> 
>   string(7) "nothing"
>   string(8) "anything"

There *is* a semantic difference.  He did not call str_replace() with array 
parameters, though, nor did he suggest so.


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#12845

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-02-16 17:41 +0100
Message-ID<5300ea1d$0$6658$9b4e6d93@newsspool2.arcor-online.net>
In reply to#12844
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
> 
>> Jason C wrote:
>>> 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);
>>> }
>>>
>>> […]
>>> 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?
>>
>> Having the fastest option is surely less important than having a correct
>> solution, and there is an important semantic difference between
>> str_replace() and strtr().  Consider the following snippet:
>>
>>   $array = array('some' => 'any', 'anything' => 'nothing');
>>   $name = 'something';
>>
>>   $keys = array_keys($array);
>>   $values = array_values($array);
>>   var_dump(str_replace($keys, $values, $name));
>>
>>   var_dump(strtr($name, $array));
>>
>> This will output:
>>
>>   string(7) "nothing"
>>   string(8) "anything"
> 
> There *is* a semantic difference.  He did not call str_replace() with array 
> parameters, though, nor did he suggest so.

Well, calling str_replace() in a loop as done by the OP has the same
semantic as calling str_replace() once with array parameters:

  $array = array('some' => 'any', 'anything' => 'nothing');
  $name = 'something';
  foreach ($array as $key => $value) {
    $name = str_replace($key, $value, $name);
  }
  var_dump($name); // string(7) "nothing"

(Using '****' instead of $value in the str_replace() might have similar
issues.)

Anyway, it is not clear to me what the OP wants to accomplish exactly.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#12843

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-02-16 10:08 -0500
Message-ID<ldqk9j$c19$1@dont-email.me>
In reply to#12837
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?-

[toc] | [prev] | [next] | [standalone]


#12846

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-16 19:21 +0000
Message-ID<0.1ee34674e35175ac7e49.20140216192157GMT.87d2im7tii.fsf@bsb.me.uk>
In reply to#12843
Norman Peelman <npeelman@cfl.rr.com> writes:

> On 02/16/2014 04:49 AM, Jason C wrote:
<snip>
>> 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.

Did you try it?  In my tests it's faster (37ms vs 66ms).  This may be
because of the assignment always being done.

I am not saying you could not have got different numbers, just that the
only way to be sure is to test with the real data in the real
environment.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.php


csiph-web