note 112363 deleted from function.preg-replace by cmb
| From: | cmb@php.net | Date: | Mon, 21 Nov 2016 19:40:32 +0000 |
| Subject: | note 112363 deleted from function.preg-replace by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207310@lists.php.net to get a copy of this message | ||
Note Submitter: jas at rephunter dot net
----
It is recommended that str_replace should be used instead of preg_replace when regex is not needed.
In particular, at http://php.net/manual/en/function.str-replace.php
it says "If you don't need fancy replacing rules (like regular expressions), you should
always use this function instead of preg_replace()."
While this is usually true, I have found a significant exception to this guideline and benchmarked
to show that str_replace is in fact slower in the case when the pattern to be replaced is an array.
In particular, when the number of elements in the pattern gets to be around 7, then the preg_replace
is faster. Of course this would depend on the specifics of the regex.
In my case I had pattern arrays with about 40 elements. This becomes far slower with str_replace
when there is a simple regex that will do the job.
Below is the benchmark with some examples of arrays for patterns and an equivalent regex.
<?php
/**
* Title: Test Harness
* Author: **********
* Date: 27-May-13
* Project: *********
* Purpose: Test timer comparing str_replace and preg_replace
*
* Results:
*
* 1. preg_replace is a lot faster!
* empty loop takes 0.12380504608154 microseconds.
* Array size: 34
* *** str_replace: 6.0163598060608 microseconds.
* *** preg_replace: 2.1114869117737 microseconds.
*
* 2. str_replace is faster:
* empty loop takes 0.11837291717529 microseconds.
* Array size: 3
* *** str_replace: 1.7525472640991 microseconds.
* *** preg_replace: 2.0717389583588 microseconds.
*
* 3. preg_replace is faster:
* empty loop takes 0.11982989311218 microseconds.
* Array size: 10
* *** str_replace: 2.6692891120911 microseconds.
* *** preg_replace: 2.2716360092163 microseconds.
*
* 3. about the same: 6 element array is breakeven point to switch to preg_replace
* empty loop takes 0.12036299705505 microseconds.
* Array size: 6
* *** str_replace: 2.0874700546265 microseconds.
* *** preg_replace: 2.1840009689331 microseconds.
*
*/
$iterations = 1000000;
$str1 = 'this is a - test';
// empty loop
$begin = microtime(true);
for ($i = 0; $i < $iterations; $i++)
{
}
$end = microtime(true);
$empty_loop_time = $end - $begin;
echo "empty loop takes $empty_loop_time microseconds.\n";
// test1 loop
// alternate array variations
//$aStr =
array('\r\n','\r','\n','%','.','(',')',':',';','&',"\x07","\x15",'!','\'','"',
'#','^','*','_','=','+','\\','?','|','<','>','{','}','â','`','~','â','?','$');
//$aStr = array('\r\n','\r','\n');
//$aStr =
array('0','1','2','3','4','5','6','7','8','9');
$aStr =
array('0','1','2','3','4','5','6');
$cnt = count($aStr);
echo 'Array size: ' . $cnt . "\n";
$begin = microtime(true);
for ($i = 0; $i < $iterations; $i++)
{
$str2 = str_replace($aStr,'',$str1);
}
$end = microtime(true);
$test1_loop_time = $end - $begin;
echo ' *** str_replace: ';
$test_time = ($test1_loop_time - $empty_loop_time) * 1000000 / $iterations;
echo "$test_time microseconds.\n";
// test2 loop
$begin = microtime(true);
for ($i = 0; $i < $iterations; $i++)
{
$str2 = preg_replace('/[^a-zA-Z\s]/','',$str1);
}
$end = microtime(true);
$test2_loop_time = $end - $begin;
echo ' *** preg_replace: ';
$test_time = ($test2_loop_time - $empty_loop_time) * 1000000 / $iterations;
echo "$test_time microseconds.\n";
?>