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

Source: Internet
Author: User
Previously analyzed STRTR's source code, now compare Strtr, str_replace and preg_replace efficiency:
Copy Code code 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 the output of the first digit at 1 o'clock, when the second digit is output for 8 o'clock

From the output results, Str_replace's overall performance relative to STRTR and preg_replace better. Reason from view Str_replace source code (http://code.google.com/p/cyy0523xc/source/browse/trunk/php/str_replace%E6%BA%90%E7%A0%81. c) can be seen, Str_replace (array search, String|array Replace, string subject) Each element of search is cycled in sequence (not according to subscript or in any other order, the array is related to the underlying implementation) and then to the subject to match, if found, replaced with the corresponding replace. In this way from the efficiency is indeed better 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 change is relatively large, and subject string is relatively long, the overhead is relatively large. But there's one thing we need to be aware of in Str_replace's implementation, which is that it won't be as big as the STRTR. For example:
Copy Code code as follows:

Str_replace (Array (' AB ', ' abc '), ' 1 ', ' ABCD ');

If we use STRTR, our output will be "1d" because the STRTR will achieve the maximum match. But Str_replace will output "1CD", because in the search string ' ab ' is in front of "ABC", so will replace ' ab ' with ' 1 '.

Now summarize the use of these three functions:
Str_replace:
This should be the preferred method for string substitution, but one thing to note is to put the elements that you most want to match in the front. (in order to improve efficiency, sometimes it is worthwhile to do so)

STRTR:STRTR is also very efficient when it comes to short strings, but the difference in the subscript length of the search array also has a greater effect on efficiency, and there is nothing better than using Strtr (string, String, String) This form (for non single-byte characters can easily produce garbled).

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

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.