PHP里strtr()为什么效率会比str_replace()快四倍,该如何解决

WBOY
Release: 2016-06-13 13:44:41
Original
1105 people have browsed it

PHP里strtr()为什么效率会比str_replace()快四倍
PHP里strtr()为什么会比str_replace()快四倍
它们的替换功能是如何实现的?什么算法?
麻烦哪位大牛,帮忙详解一下!

------解决方案--------------------
我好像总是提反对意见......再来一次.

楼上好像都默认楼主说的"strtr比str_replace快四倍"的说法是正确的....
现在网上搜了一下, 英文的没有找到这种说法, 中文的有不少,不过大部分都加了"据说"二字,

然后在机器上试了一下:

php -r ' echo microtime(true);for($i=0;$i"TEST")); }; echo "\n".$x."\n".microtime(true); '

1304349351.8423
just a TEST
1304349351.8648

php -r ' echo microtime(true);for($i=0;$i
1304349393.8981
just a TEST
1304349393.9116

好像.....没有....四倍...那么多....而且好像str_replace更快??!


我不敢就这么简单测试了就说这个说法错了,但是大家都在机子上试试吧, 

欢迎给出更多的测试例子和结果



------解决方案--------------------
贴出strtr的字符串替换源代码

C/C++ code
/* {{{ php_strtr
 */
PHPAPI char *php_strtr(char *str, int len, char *str_from, char *str_to, int trlen)
{
    int i;
    unsigned char xlat[256];

    if ((trlen 
                 
              
              
        
            
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template