Compare the efficiency of strtr, str_replace, and preg_replace functions
Source: Internet
Author: User
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, 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. The reason can be seen from the str_replace source code (http://code.google.com/p/cyy0523xc/source/browse/trunk/php/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 ), 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, but one thing to note is to put the element that most needs to be matched in front. (Sometimes it is worthwhile to improve efficiency)
Strtr:Strtr is also very efficient when replacing short strings, but the difference in the subscript length of the search array also has a big impact on efficiency, and it is best not to use strtr (string, string, string ).
Preg_replace:Needless to say, you can use regular expression matching, which is absolutely the most powerful, but it also sacrifices a little efficiency.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.