PHP: Do NOT rebuild preg_* functions!

In a PHP Project i am currently helping out, i found this function:

/**
 * Replaces every char in $str that does is not in $valid array
 *
 * @param array $valid
 * @param string $str
 * @return string
 */

function cleanString($valid, $str) {
  $ret = '';
  $len = strlen($str);
  for($i = 0; $i < $len; ++$i) {
    if(in_array($str[$i], $valid, true)) {
      $ret .= $str[$i];
    }
  }
  return $ret;
}

It is a simple function for replacing chars. It works quite well, but was mainly only used for checking [^a-z0-9]. So i tested the speed of this code.

Benchmark script

Wrote this short bench script:
http://juliusbeckmann.de/static/scripts/bench_strcheck.phps
http://juliusbeckmann.de/static/scripts/bench_strcheck.php.txt

Result

The result of it is impressive.

php bench_strcheck.php
Test: Rebuilding preg_replace function:

gstzfg87i86ef6798zwefogflwef
should be the same like:
gstzfg87i86ef6798zwefogflwef

1.039 seconds - preg_replace()
1.117 seconds - preg_cleanString()
12.160 seconds - cleanString()

The pure preg_replace was the fastes, as expected.
The capsuled preg function inside preg_cleanString was 7,5% slower than direct. 7.5% just for calling a non void function with 1 parameter is too much in my opinion.
The cleanString() functions was, as expected, the slowest of the three. But 1070% more time is way too much.

Conclusion

My conclusion is:
Do NOT rewrite optimized functions like preg_replace and others. The will not be faster. Only in case preg_* functions are not available, a replacement functions is acceptable.

No related posts.


 
 
 

2 Kommentare zu “PHP: Do NOT rebuild preg_* functions!”

  1. Jani Hartikainen 23. Dezember 2009 um 16:52

    Wow, the difference is surprisingly large - wouldn't have expected it.

    If you somehow run into a PHP installation without preg_* functions, be sure to try ereg_* ones. While they aren't as fast as preg_*, they still should beat the alternative.

  2. Julius 23. Dezember 2009 um 16:59

    I think ereg* function are even worse than own functions , because all the ereg* stuff is DEPRACED and will be remove with PHP6.

    It can be found in the documentation of PHP:
    http://php.net/ereg