This article provides a detailed analysis of the efficiency of strtr, str_replace, and preg_replace functions. For more information, see
This article provides a detailed analysis of the efficiency of strtr, str_replace, and preg_replace functions. For more information, see
The source code of strtr has been analyzed before. Now we can compare the efficiency of strtr, str_replace and preg_replace:
The Code is as follows:
$ Str =
'2017
';
$ Str = str_repeat ($ str, 1 );
$ Pattern1 = array ('000000' => '', '000000' => '');
$ Pattern2 = array ('A' => '', '000000' => '');
$ Pattern3 = '/ 12345 | 67891 /';
$ Pattern4 = '/a | 1234567890 /';
$ Pattern5 = array ('20140901', '20160901 ');
$ Pattern6 = array ('A', '201312 ');
$ T = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Strtr ($ str, $ pattern1 );
}
Echo microtime (true)-$ t, "/n"; // 0.21915886878967 0.47268319129944
$ T = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Strtr ($ str, $ pattern2 );
}
Echo microtime (true)-$ t, "/n"; // 0.4768660068512 2.7257590293884
$ T = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Preg_replace ($ pattern3, '', $ str );
}
Echo microtime (true)-$ t, "/n"; // 0.30504012107849 1.0864448547363
$ T = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Preg_replace ($ pattern4, '', $ str );
}
Echo microtime (true)-$ t, "/n"; // 0.30298089981079 1.117014169693
$ T = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Str_replace ($ pattern5, '', $ str );
}
Echo microtime (true)-$ t, "/n"; // 0.18029189109802 0.22510504722595
$ T = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Str_replace ($ pattern6, '', $ str );
}
Echo microtime (true)-$ t, "/n"; // 0.18104100227356 0.23055601119995
// Description: when the second parameter of str_repeat is 1, the first number is output. When the second parameter is 8, the second number is output.
From the output results, the overall performance of str_replace is better than that of strtr and preg_replace. View the source code of str_replace (% E6 % BA % 90% E7 % A0 % 81.c). str_replace (array search, string | array replace, string subject) during execution, each search element is cyclically ordered (not by subscript or other order, which is related to the implementation of the array at the underlying layer). Hong Kong Space, then, go to the subject to match. If it is found, replace it with the corresponding replace. In this way, the efficiency is indeed better than strtr, because there will be an extra cycle from the maximum length of the underlying object to the minimum length. If the length of the subscript string changes significantly at this time, if the subject string is long, the overhead is large. However, the implementation of str_replace also has a point that we need to pay attention to, that is, it does not have the maximum matching priority as strtr does. For example:
The Code is as follows:
Str_replace (array ('AB', 'abc'), '1', 'abcd ');
If strtr is used, the output result will be "1d", because strtr will implement the maximum matching. However, str_replace outputs "1cd". Because 'AB' in the search string is placed before "abc", 'AB' is replaced with '1' first '.
Now let's summarize the usage of these three functions:
Str_replace: this should be the preferred method for string replacement, Hong Kong space. However, it should be noted that the element most desired to be matched should be put in front. (For efficiency improvement, it is worthwhile to do so in Hong Kong Space)
Strtr: strtr is quite efficient when replacing short strings, but the difference in the subscript length of the search array also has a big impact on efficiency, also, it is best not to use the strtr (string, string, string) format (for non-single-byte characters, it is easy to produce garbled characters ).
Preg_replace: Needless to say, regular expression matching is the most powerful function, but it also sacrifices a little efficiency.