The Storyteller

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

April 30, 2007 · 14 Comments

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

Categories: PHP · idea

14 responses so far ↓

  • Dennis // April 30, 2007 at 2:21 am | Reply

    Hello,

    This is just to remind that with PHP5 you can use microtime(true) to get a float value at once (hence no need for microtime_float() function)

  • Samiha Esha // April 30, 2007 at 2:40 am | Reply

    Great :)

  • Arafat Rahman // April 30, 2007 at 11:05 am | Reply

    Great :)
    You wrote ‘\n’ at the end of ‘echo()’. Should you not write ” ?

  • Arafat Rahman // April 30, 2007 at 11:17 am | Reply

    Sorry, I was trying to write HTML br tag.
    Should you not write < br > ?

  • hasin // April 30, 2007 at 1:01 pm | Reply

    Hi arafat, I run the script from CLI, so I didn’t need that.

  • Shahar Evron // April 30, 2007 at 1:55 pm | Reply

    Hi,

    Cool benchmark – but you forgot the function specifically designed for this task: substr_count() ( http://php.net/substr_cnt ). I didn’t benchmark but it should be the fastest function of them all – and if it’s not, it should be fixed ;)

  • hasin // April 30, 2007 at 2:13 pm | Reply

    Hi Evron, Thanks for the tips. I update the code and benchmark result.

  • PHPDeveloper.org // April 30, 2007 at 7:05 pm | Reply

    Hasin Hayder’s Blog: Counting occurrence of a word in a String – Benchmarking of PHP functions

  • developercast.com » Hasin Hayder’s Blog: Counting occurrence of a word in a String - Benchmarking of PHP functions // April 30, 2007 at 10:25 pm | Reply

    [...] ever-growing list of “keeping it simple” benchmarks out there, Hasin Hayder presents his own results for the task of fining the number of times a word occurs in a given string. Today I was just [...]

  • Rahman // May 12, 2007 at 1:38 am | Reply

    Thank Hasin. Its really Coooool.

  • The Spanner - Increasing PHP performance // July 16, 2007 at 6:07 pm | Reply

    [...] I found this excellent site with some benchmark tests:- PHP benchmarks Also this on string benchmarking:- String benchmarks [...]

  • Whoila Blog » Blog Archive » Fastest way to search for string using PHP // February 20, 2009 at 4:17 pm | Reply

    [...] http://hasin.wordpress.com/2007/04/30/counting-occurrence-of-a-word-in-a-string-benchmarking-of-php-... [...]

  • Kenneth // September 5, 2009 at 3:02 am | Reply

    Excellent benchmark, this was very useful.

  • t31os // November 22, 2009 at 9:54 am | Reply

    Googled for PHP functions relating to counting occurances of text in a string, first valid and accurate result i found was here.

    Info is more then i needed, but it’s nice to see someone who tests the differences, rather then simply saying “Use this way it’s faster”..

Leave a Comment