zoukankan      html  css  js  c++  java
  • php测试for/while/foreach循环速度对比

    对比代码先行贴上,有疑问或者有不同见解的希望可以提出,大家共同进步:

    //-------------------------------------
    $k=0;
    $checkTime = ['for'=>0,'while'=>0,'foreach'=>0];
    while($k<200){
    $arr = range(0,1000000);
    $countArr=count($arr);
    $begintime = microtime();
    for($i=0;$i<$countArr;$i++){
    }
    $endtime = microtime();
    $forTime = ($endtime - $begintime);

    $begintime = microtime();
    $i=0;
    while ($i<$countArr){
    $i++;
    }
    $endtime = microtime();
    $whileTime = ($endtime-$begintime);
    $begintime = microtime();
    foreach($arr AS $key=>$val){

    }
    $endtime = microtime();
    $foreachTime= ($endtime-$begintime);
    echo 'fortime:'.$forTime.'----whiletime:'.$whileTime.'------foreachtime'.$foreachTime;
    $getMinArr = [$forTime,$whileTime,$foreachTime];
    $mintime = min($getMinArr);
    echo '<br />';
    switch($mintime){
    case $forTime:
    echo 'forTime最慢,运行时间为:'.$mintime;
    $checkTime['for']++;
    break;
    case $whileTime:
    echo 'while最慢,运行时间为:'.$mintime;
    $checkTime['while']++;
    break;
    case $foreachTime:
    echo 'foreachTime最慢,运行时间为:'.$mintime;
    $checkTime['foreach']++;
    break;
    }
    echo '<hr />';
    $k++;
    }
    echo '总结数据:-for最快速度次数:'.$checkTime['for'].';-while最快速度次数:'.$checkTime['while'].';foreach最快速度次数:'.$checkTime['foreach'];
    //---------------------------

    说明:创建100万个数组,然后用三个循环来进行对比,最后用毫秒级别时间戳来进行时间差计算。

    测试结果部分数据:

    //----------------
    fortime:0.013788----whiletime:0.01336------foreachtime0.021404
    while最快,运行时间为:0.01336

    fortime:0.013393----whiletime:0.01358------foreachtime0.021356
    forTime最快,运行时间为:0.013393

    fortime:0.013536----whiletime:0.013592------foreachtime0.022041
    forTime最快,运行时间为:0.013536

    fortime:0.013753----whiletime:0.014442------foreachtime0.021168
    forTime最快,运行时间为:0.013753

    fortime:0.013404----whiletime:0.013742------foreachtime0.021407
    forTime最快,运行时间为:0.013404

    总结数据:-for最快速度次数:88;-while最快速度次数:108;foreach最快速度次数:4
    //-----------------
    注意:上面我for循环的时候count语句是放在for循环外计算的,很多网上资料都说foreach循环比for快,但是我看了他们的实例,并且运行了他们的实例,实际上问题就是出在for的count放循环中比较
    因为使用for的时候,如果count放在里面的话,每次循环都会统计一次,接下来我稍微修改下代码,以所谓foreach比for快的方式,大家对比一下就明白了。

    //---------------------
    $k=0;
    $checkTime = ['for'=>0,'while'=>0,'foreach'=>0];
    while($k<200){
    $arr = range(0,1000000);
    $countArr=count($arr);
    $begintime = microtime();
    for($i=0;$i<count($arr);$i++){
    }
    $endtime = microtime();
    $forTime = ($endtime - $begintime);

    $begintime = microtime();
    foreach($arr AS $key=>$val){

    }
    $endtime = microtime();
    $foreachTime= ($endtime-$begintime);
    echo 'fortime:'.$forTime.'----------foreachtime'.$foreachTime;
    $getMinArr = [$forTime,$foreachTime];
    $mintime = min($getMinArr);
    echo '<br />';
    switch($mintime){
    case $forTime:
    echo 'forTime最慢,运行时间为:'.$mintime;
    $checkTime['for']++;
    break;
    case $foreachTime:
    echo 'foreachTime最慢,运行时间为:'.$mintime;
    $checkTime['foreach']++;
    break;
    }
    echo '<hr />';
    $k++;
    }
    echo '总结数据:-for最快速度次数:'.$checkTime['for'].';foreach最快速度次数:'.$checkTime['foreach'];

    //------------------------
    得出数据结果:

    这个实例我把count放在for循环里面,所以在速度方面,直线下降了。

    所以从这些数据看来,只要你把count放在for外面,在速度方面还是优于foreach的,只是for有很大局限性的,就是对数组结构有要求的,有其他间接的

    可以提出来。

    
    
  • 相关阅读:
    Java实现 LeetCode 792 自定义字符串排序(暴力)
    Java实现 LeetCode 792 自定义字符串排序(暴力)
    asp.net session对象的持久化
    Java实现 LeetCode 791 自定义字符串排序(桶排序)
    Java实现 LeetCode 791 自定义字符串排序(桶排序)
    Java实现 LeetCode 791 自定义字符串排序(桶排序)
    Java实现 LeetCode 790 多米诺和托米诺平铺(递推)
    Java实现 LeetCode 790 多米诺和托米诺平铺(递推)
    Java实现 LeetCode 790 多米诺和托米诺平铺(递推)
    小白也能看懂的约瑟夫环问题
  • 原文地址:https://www.cnblogs.com/yifan72/p/7027706.html
Copyright © 2011-2022 走看看