Counting the occurence of a word within a string: Benchmark

I added this article because when I was only trying to look up how to do this, this nut "the Storyteller" went and carried out a benchmark test on the most popular ways of counting the occurrence of a specific word within a string of text.

Source: http://hasin.wordpress.com/2007/04/30/c … functions

copyraw
<?
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”;

$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”;
     
?>
  1.  <? 
  2.  function microtime_float() { 
  3.       list($usec, $sec) = explode(“ ”, microtime())
  4.       return ((float)$usec + (float)$sec)
  5.  } 
  6.  $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’”; 
  7.   
  8.  $start = microtime_float()
  9.  for ($i=0$i<10000$i++) { 
  10.       $cnt = count(split(“PHP”,$str))-1
  11.  } 
  12.  $end = microtime_float()
  13.  echo “Count by Split+Count took : ”.($end-$start).“Seconds”; 
  14.   
  15.  $start = microtime_float()
  16.  for ($i=0$i<10000$i++) 
  17.  { 
  18.       preg_match_all(“/php/i”,$str,$matches)
  19.   
  20.       $cnt = count($matches[0])
  21.   
  22.  } 
  23.  $end = microtime_float()
  24.  echo “Count by Preg_Match+Count took : ”.($end-$start).“ Seconds\n”; 
  25.   
  26.  $start = microtime_float()
  27.   
  28.  for ($i=0$i<10000$i++) 
  29.  { 
  30.   
  31.       str_replace(“PHP”,“PP”,$str,$cnt)
  32.       //echo $cnt; 
  33.  } 
  34.  $end = microtime_float()
  35.   
  36.  echo “Count by str_replace took : ”.($end-$start).“ Seconds\n”; 
  37.   
  38.  $start = microtime_float()
  39.   
  40.  for ($i=0$i<10000$i++) 
  41.  { 
  42.       str_ireplace(“PHP”,“PP”,$str,$cnt)
  43.   
  44.       //echo $cnt; 
  45.  } 
  46.  $end = microtime_float()
  47.  echo “Count By str_ireplace took : ”.($end-$start).“ Seconds\n”; 
  48.   
  49.  $start = microtime_float()
  50.   
  51.  for ($i=0$i<10000$i++) 
  52.  { 
  53.   
  54.       $cnt = count(explode(“PHP”,$str))-1
  55.       //echo $cnt; 
  56.  } 
  57.  $end = microtime_float()
  58.   
  59.  echo “Count By Explode+Count took : ”.($end-$start).“ Seconds\n”; 
  60.   
  61.  $start = microtime_float()
  62.   
  63.  for ($i=0$i<10000$i++) 
  64.  { 
  65.       $word_count = (array_count_values(str_word_count(strtolower($str),1)))
  66.   
  67.       ksort($word_count)
  68.   
  69.       $cnt = $word_count['php']
  70.  } 
  71.  $end = microtime_float()
  72.  echo “Count By Array Functions took : ”.($end-$start).“ Seconds\n”; 
  73.   
  74.  $start = microtime_float()
  75.  for ($i=0$i<10000$i++) 
  76.   
  77.  { 
  78.       $cnt = count(preg_split(“/PHP/i”,$str))-1
  79.  } 
  80.  $end = microtime_float()
  81.   
  82.  echo “Count By preg_split+Count took : ”.($end-$start).“ Seconds\n”; 
  83.   
  84.  $start = microtime_float()
  85.  for ($i=0$i<10000$i++) 
  86.  { 
  87.   
  88.       $cnt = substr_count($str, “PHP”)
  89.  } 
  90.  $end = microtime_float()
  91.  echo “Count By substr_count took : ”.($end-$start).“ Seconds\n”; 
  92.   
  93.  ?> 

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

Source: http://hasin.wordpress.com/2007/04/30/c … functions

 

 

Category: Personal Home Page :: Article: 176

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

Please publish modules in offcanvas position.