源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-01-26 03:35 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:深入for,while,foreach遍历时间比较的详解
这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。
[u]复制代码[/u] 代码如下:
<?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 />'; ?>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部