Welcome Guest, Not a member yet? Register   Sign In
Things that affect our applications' performance
#8

[eluser]Seppo[/eluser]
[quote author="Derek Jones" date="1214772129"]Seppo, can you share your code, environment details, and results? I don't see anything of the sort. strpos() if there are more matches than not is a tiny bit slower, but affords much greater speed when the replace item might not exist. And an array replacement with calls to fetch keys and values is always much much slower on every environment I have used, whether the replace strings exist or not, by orders of magnitude.[/quote]

Derek,
I've retested with a longer string (700 KB file) and I was thinking how to apologize Tongue. After that, I tested with a smaller string (186 KB) and I got a solution more like I expected, and with a more usual PHP string (like 100 chars or less) the results were exactly as I expected.
I'm running PHP 5.1.1 under Windows XP.

The code:
Code:
<?php
//$str = file_get_contents('/path/to/a/long/file');
$str = 'Hello! I am a small text. Hello! I am a small text. Hello! I am a small text.';
$items = array(
    'a' => 'b', // Many, many times
    'am' => 'd', // A few times
    'qweasdzxchiasfoihfiohioh' => '28946918yhsdouiahydoi' // Unlikely
);
foreach ($items as $key => $value)
{
    echo $key , ': ' , substr_count($str, $key) , "\n";
}
echo "\n";

$start = microtime(true);
for ($a = 0; ++$a < 100;)
{
    foreach ($items as $name => $value)
    {
        if (strpos($str, $name) !== FALSE)
        {
            str_replace($name, $value, $str);
        }
    }
}

echo 'Using strpos: ' , microtime(true) - $start;
echo "\n";

$start = microtime(true);
for ($a = 0; ++$a < 100;)
{
    foreach ($items as $name => $value)
    {
        str_replace($name, $value, $str);
    }
}
echo 'Not using strpos: ', microtime(true) - $start;
echo "\n";

$start = microtime(true);
for ($a = 0; ++$a < 100;)
{
    str_replace(array_keys($items), array_values($items), $str);
}
echo 'Using array: ', microtime(true) - $start;

With the 700kb file I'm getting:
Quote:a: 1190
am: 23
qweasdzxchiasfoihfiohioh: 0

Using strpos: 1.1060960292816
Not using strpos: 1.377170085907
Using array: 1.2317478656769

186KB
Quote:a: 674
am: 2
qweasdzxchiasfoihfiohioh: 0

Using strpos: 0.25286197662354
Not using strpos: 0.21404910087585
Using array: 0.22534990310669

'Hello! I am a small text. Hello! I am a small text. Hello! I am a small text.'
Quote:a: 9
am: 3
qweasdzxchiasfoihfiohioh: 0

Using strpos: 0.0062811374664307
Not using strpos: 0.0050199031829834
Using array: 0.0021741390228271


Messages In This Thread
Things that affect our applications' performance - by El Forum - 06-29-2008, 05:57 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 07:51 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 09:07 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 09:21 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 09:42 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 11:42 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 11:56 AM
Things that affect our applications' performance - by El Forum - 06-29-2008, 01:38 PM
Things that affect our applications' performance - by El Forum - 06-29-2008, 03:40 PM
Things that affect our applications' performance - by El Forum - 06-29-2008, 03:46 PM
Things that affect our applications' performance - by El Forum - 06-29-2008, 05:11 PM
Things that affect our applications' performance - by El Forum - 06-29-2008, 05:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB