Compare the efficiency of STRTR, Str_replace and preg_replace three functions _php tutorial

Source: Internet
Author: User
Have analyzed the source of strtr before, now compare Strtr, str_replace and preg_replace efficiency:
Copy CodeThe code is as follows:
$str =
' 111111110000000000000000000000000000000111000001000100010000010010000010010000010100000010
';
$str = Str_repeat ($str, 1);
$pattern 1 = Array (' 12345 ' = = ', ' 67891 ' = ');
$pattern 2 = Array (' a ' = = ', ' 1234567890 ' = ');
$pattern 3 = '/12345|67891/';
$pattern 4 = '/a|1234567890/';
$pattern 5 = Array (' 12345 ', ' 67891 ');
$pattern 6 = Array (' A ', ' 1234567890 ');
$t = Microtime (true);
for ($i =0; $i <10000; $i + +)
{
STRTR ($str, $pattern 1);
}
Echo Microtime (True)-$t, "/n"; 0.21915886878967 0.47268319129944
$t = Microtime (true);
for ($i =0; $i <10000; $i + +)
{
STRTR ($str, $pattern 2);
}
Echo Microtime (True)-$t, "/n"; 0.4768660068512 2.7257590293884
$t = Microtime (true);
for ($i =0; $i <10000; $i + +)
{
Preg_replace ($pattern 3, ", $STR);
}
Echo Microtime (True)-$t, "/n"; 0.30504012107849 1.0864448547363
$t = Microtime (true);
for ($i =0; $i <10000; $i + +)
{
Preg_replace ($pattern 4, ", $STR);
}
Echo Microtime (True)-$t, "/n"; 0.30298089981079 1.117014169693
$t = Microtime (true);
for ($i =0; $i <10000; $i + +)
{
Str_replace ($pattern 5, ", $STR);
}
Echo Microtime (True)-$t, "/n"; 0.18029189109802 0.22510504722595
$t = Microtime (true);
for ($i =0; $i <10000; $i + +)
{
Str_replace ($pattern 6, ", $STR);
}
Echo Microtime (True)-$t, "/n"; 0.18104100227356 0.23055601119995
Description: When the second parameter of str_repeat is 1 o'clock output the first number, when the second number is output for 8 o'clock

From the output results, the overall performance of Str_replace is better than Strtr and Preg_replace. Reasons to view Str_replace from the source code (http://code.google.com/p/cyy0523xc/source/browse/trunk/php/str_replace%E6%BA%90%E7%A0%81. c) It can be seen that Str_replace (array search, String|array Replace, string subject) Each element of search is executed in chronological order (not according to subscript or other order, the array is related to the underlying implementation), then to subject, and then to the corresponding replace if found. This will be more efficient than STRTR, because there will be more than the maximum length from the subscript to the minimum length of the cycle, if this time the length of the label string changes relatively large, and the subject string is longer, the cost here is relatively large. But there is one thing we need to be aware of in this implementation of Str_replace, which is that it does not take precedence over the same as STRTR. For example:
Copy CodeThe code is as follows:
Str_replace (Array (' AB ', ' abc '), ' 1 ', ' ABCD ');

If you are using STRTR, we will output the result "1d", because STRTR will achieve the maximum match. But Str_replace will output "1CD", because in the search string ' ab ' is in front of "ABC", so the ' ab ' is replaced with ' 1 '.

Now let's summarize the usage of these three functions:
Str_replace:
This should be the preferred method for string substitution, but one thing to keep in mind is to put the elements that you want to match first. (It is sometimes worthwhile to do so in order to improve efficiency.)

STRTR:STRTR in short string substitution is also very efficient, but the search array of the subscript length of the difference also has a greater impact on efficiency, there is nothing better not to use STRTR (string, String, String) This form (which is easily garbled for non-single-byte characters).

Preg_replace:This needless to say, you can use regular matching, the function is absolutely the strongest, but also to sacrifice a little efficiency.

http://www.bkjia.com/PHPjc/327903.html www.bkjia.com true http://www.bkjia.com/PHPjc/327903.html techarticle previously analyzed the source of STRTR, now compare Strtr, str_replace and preg_replace efficiency: Copy code code as follows: $str = ' 11111111000000000000000000000000000000011100 ...

  • Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.