2010年10月19日 星期二

[php] Stristr & preg_match 速度比較

run 100萬次的情況下.
結果:
run times :1000000
preg_match time: 2.2464230061 seconds.
stristr time: 3.1119530201 seconds.
39% improvement.


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

$startTime = timeStamp();
$j = 1000000;
for($i=$j; $i > 1; $i--) {
$test = preg_match("/com/i", "PHP is the web scripting lan.comguage of choice.");
}
echo "run times :".$j."<br>";
$splitTime = timeStamp();
$splitTimeecho = round($splitTime - $startTime,10);
echo "preg_match time: ".$splitTimeecho." seconds.";

// Do some more things here
for($i=$j; $i > 1; $i--) {
$test2 = stristr("PHP is the web scripting lan.comguage of choice.", 'com');
}

$endTime = timeStamp();
$totalTime = round($endTime - $splitTime,10);
echo "<br>stristr time: ".$totalTime." seconds.<br>";
echo round(($totalTime / $splitTimeecho) * 100) - 100 . "% improvement.";
?>

【下列文章您可能也有興趣】

沒有留言: