本文實例講述了PHP中strtr與str_replace函數運行性能簡單測試。分享給大家供大家參考,具體如下:
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";
結果如下:
那個正則替換的那個就不測了,應該是趕不上這兩個的。
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選