php的foreach,while,for的性能比较

<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">关注微信公众号:wwwcoder,现在已将博客搬到这里,内容精选过后才发布出来。谢谢大家支持</span>
for ($j = 10; $j > 0; $j--) {

    $itmes = array_fill(0, 100000, '12345678910');

    $start = microtime(true);
    reset($itmes);
    foreach ($itmes as $item) {
        $x = $item;
    }
    $end[] = microtime(true) - $start;
}
echo "<br>";
echo "foreach执行10次后的平均耗时:" . array_sum($end) / count($end) . "毫秒";
echo "<br>";

for ($j = 10; $j > 0; $j--) {

    $itmes = array_fill(0, 100000, '12345678910');

    $start = microtime(true);
    reset($itmes);
    $i = 0;
    while ($i < 100000) {
        $x = $itmes[$i];
        $i++;
    }
    $end1[] = microtime(true) - $start;
}
echo "<br>";
echo "while执行10次后的平均耗时:" . array_sum($end1) / count($end1) . "毫秒";
echo "<br>";

for ($j = 10; $j > 0; $j--) {

    $itmes = array_fill(0, 100000, '12345678910');

    $start = microtime(true);
    reset($itmes);
    for ($i = 0; $i < 100000; $i++) {
        $x = $itmes[$i];
    }
    $end2[] = microtime(true) - $start;
}
echo "<br>";
echo "for执行10次后的平均耗时:" . array_sum($end2) / count($end2) . "毫秒";
echo "<br>";
执行结果:

foreach执行10次后的平均耗时:0.023235964775085毫秒
while  执行10次后的平均耗时:0.043575739860535毫秒
for    执行10次后的平均耗时:0.04455361366272毫秒
有什么不正确的地方请大家指出来,谢啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值