Counting occurrence of a word in a String :: Benchmarking of PHP functions

Today I was just thinking what are the possible ways to count the occurrence of a specific word inside a string. I found some possible ways finally and I just benchmarked them. Wanna see the result?? – for sure you will find it interesting too.

<?
function microtime_float()
{
   list(
$usec$sec) = explode(” “microtime());

   return ((float)$usec + (float)$sec);
}

$str “I have three PHP books, first one is ‘PHP Tastes Good’,
 next is ‘PHP in your breakfast’ and the last one is ‘PHP Nightmare'”
;

$start microtime_float();
for (
$i=0$i<10000$i++)
{

    $cnt count(split(“PHP”,$str))-1;
}
$end microtime_float();

echo “Count by Split+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();

for ($i=0$i<10000$i++)
{
    
preg_match_all(“/php/i”,$str,$matches);

    $cnt count($matches[0]);

}
$end microtime_float();
echo 
“Count by Preg_Match+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();

for (
$i=0$i<10000$i++)
{

    str_replace(“PHP”,“PP”,$str,$cnt);
    
//echo $cnt;
}
$end microtime_float();

echo “Count by str_replace took : “.($end$start).” Seconds\n”;

$start microtime_float();

for ($i=0$i<10000$i++)
{
    
str_ireplace(“PHP”,“PP”,$str,$cnt);

    //echo $cnt;
}
$end microtime_float();
echo 
“Count By str_ireplace took : “.($end$start).” Seconds\n”;

$start microtime_float();

for (
$i=0$i<10000$i++)
{

    $cnt count(explode(“PHP”,$str))-1;
    
//echo $cnt;
}
$end microtime_float();

echo “Count By Explode+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();

for (
$i=0$i<10000$i++)
{
    
$word_count = (array_count_values(str_word_count(strtolower($str),1)));

    ksort($word_count);

    
$cnt $word_count[‘php’];
}
$end microtime_float();
echo 
“Count By Array Functions took : “.($end$start).” Seconds\n”;

$start microtime_float();
for (
$i=0$i<10000$i++)

{
    $cnt count(preg_split(“/PHP/i”,$str))-1;
}
$end microtime_float();

echo “Count By preg_split+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();
for (
$i=0$i<10000$i++)
{

    $cnt substr_count($str“PHP”);
}
$end microtime_float();
echo 
“Count By substr_count took : “.($end$start).” Seconds\n”;

?>

And the result is

First Run
Count by Split+Count took : 0.44112181663513 Seconds
Count by Preg_Match+Count took : 0.46423101425171 Seconds
Count by str_replace took : 0.23512482643127 Seconds
Count By str_ireplace took : 0.39766597747803 Seconds
Count By Explode+Count took : 0.25045800209045 Seconds
Count By Array Functions took : 1.1077101230621 Seconds
Count By preg_split+Count took : 0.30741000175476 Seconds
Count By substr_count took : 0.21060705184937 Seconds

Second Run

Count by Split+Count took : 0.68125295639038 Seconds
Count by Preg_Match+Count took : 0.60020899772644 Seconds
Count by str_replace took : 0.2877471446991 Seconds
Count By str_ireplace took : 0.47500586509705 Seconds
Count By Explode+Count took : 0.31055402755737 Seconds
Count By Array Functions took : 1.3551599979401 Seconds
Count By preg_split+Count took : 0.40205383300781 Seconds
Count By substr_count took : 0.24432802200317 Seconds

Third Run
Count by Split+Count took : 0.50134515762329 Seconds
Count by Preg_Match+Count took : 0.53588891029358 Seconds
Count by str_replace took : 0.25469994544983 Seconds
Count By str_ireplace took : 0.34696006774902 Seconds
Count By Explode+Count took : 0.23176002502441 Seconds
Count By Array Functions took : 1.0504789352417 Seconds
Count By preg_split+Count took : 0.28686618804932 Seconds
Count By substr_count took : 0.20796585083008 Seconds

Fourth Run
Count by Split+Count took : 0.4736020565033 Seconds
Count by Preg_Match+Count took : 0.48813104629517 Seconds
Count by str_replace took : 0.29280996322632 Seconds
Count By str_ireplace took : 0.51396799087524 Seconds
Count By Explode+Count took : 0.34470105171204 Seconds
Count By Array Functions took : 1.4177949428558 Seconds
Count By preg_split+Count took : 0.36489319801331 Seconds
Count By substr_count took : 0.27841401100159 Seconds

If you are interested to know the machine configuration, these tests ran on a Celeron 1.6GHz processor based laptop with 768 MB of RAM. And I am using PHP 5.1.1

%d