深入for,while,foreach遍历时间比较的详解

前端之家收集整理的这篇文章主要介绍了深入for,while,foreach遍历时间比较的详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。
<div class="codetitle"><a style="CURSOR: pointer" data="96187" class="copybut" id="copybut96187" onclick="doCopy('code96187')"> 代码如下:

<div class="codebody" id="code96187">
<?PHP
$arr = array();
for($i = 0; $i < 50000; $i++){
$arr[] = $irand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
/
=============================================/
$time_start = GetRunTime();
for($i = 0; $i < count($arr); $i++){
$str = $arr[$i];
}
$time_end = GetRunTime();
$time_used = $time_end - $time_start;
echo 'Used time of for:'.round($time_used,7).'(s)

';
unset($str,$time_start,$time_end,$time_used);
/
=============================================/
$time_start = GetRunTime();
while(list($key,$val) = each($arr)){
$str = $val;
}
$time_end = GetRunTime();
$time_used = $time_end - $time_start;
echo 'Used time of while:'.round($time_used,$key,$val,$time_used);
/
=============================================*/
$time_start = GetRunTime();
foreach($arr as $key => $val){
$str = $val;
}
$time_end = GetRunTime();
$time_used = $time_end - $time_start;
echo 'Used time of foreach:'.round($time_used,7).'(s)

';
?>

原文链接:https://www.f2er.com/php/26376.html

猜你在找的PHP相关文章