str_replace with strpos?
Asked Answered
K

5

5

The str_replace function with a strpos checking can avoid extra work?

METHOD 1

...
if (strpos($text, $tofind) !== FALSE)
 $text = str_replace($tofind, $newreplace, $text);
...

METHOD 2

...
$text = str_replace($tofind, $newreplace, $text);
...

Question: This two methods works but... I want know if strpos-checking (or other) is good way or a bad, useless (and optimization antipattern).

Kahl answered 14/10, 2011 at 22:36 Comment(0)
P
6

You may save some str_replace() calls, but you get always additional strpos()-calls and !== false comparisons. However, I don't think, that it will make any measureable impact, as long as this code will not run around 100000 times (or such). Thus as long as you don't need to know, if there are replacements to made, you should avoid this "optimization" to keep things more simple and readable.

Ph answered 14/10, 2011 at 22:44 Comment(0)
S
5

You can always time things yourself:

$start = 0; $end = 0;

$tofind = 'brown';

// A
$start = microtime(true);
for ($a=0; $a<1000; $a++) {
    if (strpos('The quick brown fox', $tofind) !== FALSE)
        str_replace($tofind, 'red', 'The quick brown fox');

}
$end = microtime(true);
echo $end - $start . "<br />\n";

// B
$start = microtime(true);
for ($b=0; $b<1000; $b++) {
    str_replace($tofind, 'red', 'The quick brown fox');
}
$end = microtime(true);
echo $end - $start . "<br />\n";

/*
various outputs:

0.0021979808807373
0.0013730525970459

0.0020320415496826
0.00130295753479

0.002094030380249
0.0013539791107178

0.0020980834960938
0.0013020038604736

0.0020389556884766
0.0012800693511963

0.0021991729736328
0.0013909339904785

0.0021369457244873
0.0012800693511963

*/

Adding strpos is slower every time, but not by much.

A good rule of thumb is don't guess where your bottlenecks will be. Code for functionality and good, clean design. After that, you can profile when performance tests warrant it.

Sudiesudnor answered 14/10, 2011 at 22:49 Comment(2)
Don't downvote in silence. Explain them to help better answers.Sudiesudnor
"every time" - this is wrong, if you do test with lots of text and try to find something that does not exits, using strpos can be faster up to 30%. You probably wanted to say "usually".Mineral
L
4

The method without strpos is better.

Let's assume that both the strpos and the str_replace have the same worst case running time, because they both have to iterate through the whole text.

By using both, you have in the worst case, double the running time than just using str_replace alone.

Luminal answered 14/10, 2011 at 22:43 Comment(0)
H
0

I've just tested 3 ways to replace constants in my config file :

// No check
function replaceConstantsNoCheck($value)
{
    foreach (array_keys(get_defined_constants()) as $constant)
        $value = str_replace($constant, constant($constant), $value);

    return $value;
}

// Check with strstr
function replaceConstantsStrstr($value)
{
    foreach (array_keys(get_defined_constants()) as $constant)
        if (strstr($value, $constant))
            $value = str_replace($constant, constant($constant), $value);

    return $value;
}

// Check with strpos
function replaceConstantsStrpos($value)
{
    foreach (array_keys(get_defined_constants()) as $constant)
        if (strpos($value, $constant) !== false)
            $value = str_replace($constant, constant($constant), $value);

    return $value;
}

Some measurements :

/*
No check : 0.0078179836273193
Strstr   : 0.0034809112548828
Strpos   : 0.0034389495849609

No check : 0.0067379474639893
Strstr   : 0.0034348964691162
Strpos   : 0.0034480094909668

No check : 0.0064759254455566
Strstr   : 0.0031521320343018
Strpos   : 0.0032868385314941

No check : 0.0068850517272949
Strstr   : 0.003389835357666
Strpos   : 0.0031671524047852

No check : 0.006864070892334
Strstr   : 0.0032939910888672
Strpos   : 0.0032010078430176
*/

No check method used a least double of the time in all my tests !

It seems to not have significant difference between strstr and strpos methods.

Heartwood answered 16/4, 2012 at 13:33 Comment(0)
V
0

Another benchmark results here: with strpos: http://3v4l.org/pb4hY#v533 without strpos: http://3v4l.org/v35gT

Vickery answered 28/4, 2014 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.