手机
当前位置:查字典教程网 >编程开发 >php教程 >深入for,while,foreach遍历时间比较的详解
深入for,while,foreach遍历时间比较的详解
摘要:这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。复制代码代码如下:

这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。

复制代码 代码如下:

<?php

$arr = array();

for($i = 0; $i < 50000; $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 < 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)<br /><br />';

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)<br /><br />';

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)<br /><br />';

?>

【深入for,while,foreach遍历时间比较的详解】相关文章:

把PHP安装为Apache DSO

国内php原创论坛

php生成图片验证码

第1次亲密接触PHP5(1)

Oracle 常见问题解答

PHP中实现进程间通讯

PHP实现即时输出、实时输出内容方法

PHP概述.

Mysql的常用命令

MVC模式的PHP实现

精品推荐
分类导航