Why is strtr () in PHP four times faster than str_replace ()?

Source: Internet
Author: User
In PHP, why is strtr () four times faster than str_replace ()? how is the replacement function implemented? What algorithm? Which of the following is the trouble! ------ Solution -------------------- I seem to have always raised objections ...... try again. upstairs, it seems that, by default, the author said & quot; strtr is four times faster than strtr () in str _ PHP than strtr ().
Why is strtr () four times faster than str_replace () in PHP?
How are their replacement functions implemented? What algorithm?
Which of the following is the trouble!

------ Solution --------------------
I seem to always raise objections.

Upstairs, it seems that it is correct to say "strtr is four times faster than str_replace" by default ....
I searched the internet and found no such statement in English. There are many Chinese characters, but most of them contain the word "said,

Then I tried 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 that... no... four times... so many... and it seems that str_replace is faster ??!


I did not dare to give it a simple test. I just said this was wrong, but everyone tried it on the machine,

More test examples and results are welcomed.



------ Solution --------------------
Returns the strtr string replacement source code.

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 < 1) || (len < 1)) {        return str;    }    for (i = 0; i < 256; 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.