PHP strtr () Why the efficiency will be faster than Str_replace () four times times, how to solve

Source: Internet
Author: User
PHP strtr () Why efficiency is four times times faster than Str_replace ()
Why PHP Strtr () is four times times faster than Str_replace ()
How are their replacement functions implemented? What algorithm?
Excuse me, Daniel, help me to explain!

------Solution--------------------
It seems to me that I always raise objections ... One more time.

Upstairs seems to be the default landlord said the "Strtr faster than str_replace four times times" The saying is correct ....
Now search on the internet, English did not find such a statement, there are many Chinese, but most of them added the word "said",

Then try it on the machine:

Php-r ' echo microtime (true); for ($i =0; $i <10000; $i + +) {$x =strtr ("Just a Test", Array ("Test" = "test");}; echo "\ n". $x. " \ n ". Microtime (True); '

1304349351.8423
Just a TEST
1304349351.8648

Php-r ' echo microtime (true); for ($i =0; $i <10000; $i + +) {$x =str_replace ("Test", "Test", "just a Test"), echo "\ n". $x. " \ n ". Microtime (True); '

1304349393.8981
Just a TEST
1304349393.9116

It seems like..... No.... Four times times ... So many .... And it seems str_replace faster??!


I don't dare to test it this way, say this is wrong, but everyone on the machine to try it,

More test examples and results are welcome



------Solution--------------------
Paste the STRTR string to replace the source code

C + + code
/* {{{php_strtr */phpapi char *php_strtr (char *str, I    NT Len, Char *str_from, char *str_to, int trlen) {int i;    unsigned char xlat[256]; if ((Trlen < 1) | | (Len < 1))    {return str;    } for (i = 0; i <; Xlat[i] = i, i++);    for (i = 0; i < Trlen; i++) {xlat[(unsigned char) str_from[i]] = str_to[i];    } for (i = 0; i < len; i++) {Str[i] = xlat[(unsigned char) str[i]]; } return str;} /*}}} */
  • 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.