欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-15 18:45:52
...
这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。
复制代码 代码如下:

$arr = array();
for($i = 0; $i $arr[] = $i*rand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
/*=============================================*/
$time_start = GetRunTime();
for($i = 0; $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, 7).'(s)

';
unset($str, $key, $val, $time_start, $time_end, $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)

';
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327482.htmlTechArticle这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。 复制代码 代码如下: ?php $arr = array(...