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