Compare the efficiency of strtr str_replace and preg_replace

  • 2020-06-23 00:00:41
  • OfStack

The source code of strtr has been analyzed before. Now compare the efficiency of strtr, str_replace and preg_replace:

$str = 
'111111110000000000000000000000000000000111000001000100010000010010000010010000010100000010
'; 
$str = str_repeat($str, 1); 
$pattern1 = array('12345'=>'', '67891'=>''); 
$pattern2 = array('a'=>'', '1234567890'=>''); 
$pattern3 = '/12345|67891/'; 
$pattern4 = '/a|1234567890/'; 
$pattern5 = array('12345', '67891'); 
$pattern6 = array('a', '1234567890');  
$t = microtime(true); 
for($i=0; $i<10000; $i++) 
{
     strtr($str, $pattern1); 
} 
echo microtime(true)-$t, "/n";        //0.21915886878967   0.47268319129944  
$t = microtime(true); 
for($i=0; $i<10000; $i++) 
{
     strtr($str, $pattern2); 
} 
echo microtime(true)-$t, "/n";        //0.4768660068512    2.7257590293884   
$t = microtime(true); 
for($i=0; $i<10000; $i++) 
{
     preg_replace($pattern3, '', $str); 
} 
echo microtime(true)-$t, "/n";        //0.30504012107849    1.0864448547363  
$t = microtime(true); 
for($i=0; $i<10000; $i++) 
{
     preg_replace($pattern4, '', $str); 
} 
echo microtime(true)-$t, "/n";        //0.30298089981079    1.117014169693   
$t = microtime(true); 
for($i=0; $i<10000; $i++) 
{
     str_replace($pattern5, '', $str); 
} 
echo microtime(true)-$t, "/n";        //0.18029189109802    0.22510504722595  
$t = microtime(true); 
for($i=0; $i<10000; $i++) 
{
     str_replace($pattern6, '', $str); 
} 
echo microtime(true)-$t, "/n";        //0.18104100227356   0.23055601119995  
// Note: when str_repeat The first 2 The parameters for 1 When the output is the first 1 The number, when is zero 8 When the output is the first 2 A digital  

From the output, the overall performance of str_replace is better than that of strtr and preg_replace. Reasons from the view of str_replace source (http: / / code. google. com p/cyy0523xc/source/browse/trunk php/str_replace A0 90% E7 BA E6 % % % % % 81. c) can be seen, str_replace(array search, string|array replace, string subject) loops through each element of search in sequence (not in subscript or other order, which is related to the implementation of the array at the bottom), then matches to subject, and if found, replaces the corresponding replace. This is actually more efficient than strtr because there is one more loop from the maximum length to the minimum length of the subscript, which is expensive if the subscript string has a larger variation in length and subject is longer. One caveat to an implementation like str_replace, though, is that it doesn't have the maximum match priority that strtr does. Such as:

 str_replace(array('ab', 'abc'), '1', 'abcd');
 

If we were using strtr, our output would be "1d" because strtr would achieve the maximum match. But str_replace will print "1cd" because 'ab' precedes' abc' in the search string, so it replaces' ab' with '1' first.

Now let's summarize the usage of these three functions:
str_replace: This should be the preferred method of string substitution, but there is one caveat: put the elements you most want to match first. (Sometimes it's worth it for the sake of efficiency)

strtr: strtr is also efficient for short string substitutions, but the difference in the subscript length of the search array also has a big impact on efficiency. Also, it is better not to use strtr(string, string, string).

preg_replace: Needless to say, you can use regular matching, which is definitely the most powerful, but at the cost of 1 point of efficiency.

Related articles: