PHP strtr与str_replace函数性能简单测试

来源:互联网 发布:搭建阿里云服务器 编辑:程序博客网 时间:2024/06/09 20:09

strtr与str_replace函数性能,很简单的一个测试,只是简单的测下,供参考,代码如下:

<?phprequire_once('Timer.php');$target = 'qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./';$count = isset($argv[1]) ? (int)$argv[1] : 1;$needle = 'vb';Timer::getInstance()->begin();for($i = 0; $i < $count; $i++) {strtr($target, $needle, '*');}echo "strtr exec {$count} times used time: " . Timer::getInstance()->end()->gone() . " sec.\n";//----------------------------------------------------------------------------------------------Timer::getInstance()->begin();for($i = 0; $i < $count; $i++) {str_replace($needle, '*', $target);}echo "str_replace exec {$count} times used time: " . Timer::getInstance()->end()->gone() . " sec.\n";

结果如下:


那个正则替换的那个就不测了,应该是赶不上这两个的。

0 0
原创粉丝点击