Compare Strtr, str

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 str_repeat second parameter outputs the first number for 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 you are using STRTR, the result we output will be "1d" because the STRTR will achieve a 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 usage of these three functions:
str_replace:
This should be the preferred method for string substitution, but one thing to note, is to put the most desired matching elements in the front. (Sometimes it's worth doing this for the sake of efficiency)

strtr: Strtr is also very efficient when short strings are replaced, but the difference in the subscript length of the search array also has a greater effect on efficiency, There's nothing else. It is best not to use STRTR (string, String, string) in this form (easily garbled for non single-byte characters).

preg_replace: This goes without saying that you can use regular matching, which is definitely the strongest feature, but it also has to be a little more efficient.

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.