| This article introduces three strtr, str_replace, and preg_replace functions used for string processing in php to compare and analyze the efficiency. For more information, see. The following compares the efficiency of strtr, str_replace, and preg_replace:
'', '000000' =>''); $ pattern2 = array ('a' => '', '000000' => ''); $ pattern3 = '/12345 | 67891/'; $ pattern4 = '/a | 1234567890/'; $ pattern5 = array ('20140901', '20140901 '); $ pattern6 = array ('A', '000000'); $ t = microtime (true); for ($ I = 0; $ I <1234567890; $ 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 $ t = microtime (true); for ($ I = 0; $ I <10000; $ I ++) {preg_replace ($ pattern3, '', $ str);} echo microtime (true)-$ t,"/n "; // 0.30504012107849 1.0864448547363 $ t = microtime (true); for ($ I = 0; $ I <10000; $ I ++) {preg_replace ($ pattern4 ,'', $ str);} echo microtime (true)-$ t, "/n"; // 0.30298089981079 $ t = microtime (tru E); for ($ I = 0; $ I <10000; $ I ++) {str_replace ($ pattern5, '', $ str);} echo microtime (true) -$ t, "/n"; // 0.18029189109802 0.22510504722595 $ t = microtime (true); for ($ I = 0; $ 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 value 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:
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 '. To sum up the usage of the following three functions: 1, str_replace: this should be the preferred method for string replacement. Note that you should put the elements that you most want to match in front. 2. 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 ). 3. preg_replace: you can use regular expression matching. the function is definitely the most powerful and the efficiency is a little lower. |