1. 程式人生 > >PHP中的stristr(),strstr(),strpos()速度比較

PHP中的stristr(),strstr(),strpos()速度比較

lod end 寫敏感 一次 strpos function rpo 明顯 art

測速代碼:

<?php
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$time_start = getmicrotime();
$string="xxxddxx";
$find="d";
for ($i=0;$i<300000;$i++)
{
if (stristr($string,$find))//if (strstr($string,$find)) or if (strpos($string,$find))
{}
}

$time_end = getmicrotime();
echo $time_end-$time_start;
?>

[/php]


stristr()
第一次:2.74142408371
第二次:2.52075314522
第三次:2.52766990662

strstr()
第一次:1.43941402435
第二次:1.44914388657
第三次:1.51285290718

strpos()
第一次:1.42109084129
第二次:1.40254187584
第三次:1.38609910011
----------------------------------
可以看出,stristr在進行判斷某一字符(串)是否存在於另一字符(串)時候,速度明顯慢於另外兩個。。
stristr對大小寫不敏感的
strstr對大小寫敏感
strpos不能判斷是否有特殊字符(包括中文字符)

呵呵,以後用的時候記得選擇好啦。。

PHP中的stristr(),strstr(),strpos()速度比較