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;}/* }}} */