免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1983 | 回复: 0
打印 上一主题 下一主题

递归效率比较 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-17 11:03 |只看该作者 |倒序浏览

                                纯属个人兴趣,比较了一下递归的效率
Windows下:apache+php
递归时间:  1290次  0.0127840042114 times
非递归时间:1290次  0.00402212142944 times
Linux下:nginx+php
递归时间:  22290次  0.0981810092926 times
非递归时间:22290次  0.0421569347382 times
在Windows下,是我自己的机器,关于如何配置,让递归层数更大,我还没研究明白到底是哪里的配置有关,但是我相信是APACHE的配置关系到的. 我自己的机器,最大层数就是1290次.相信是配置优化的问题.
PHP.INI配置
memory_limit = 1000M      ; Maximum amount of memory a script may consume (128MB)
该配置确实影响递归层数,因为这是要分配内存的大小.如果超出,会报PHP的超出内存的错误.
但是设置为以上参数时
超出1290次后,我的APACHE会报如下错误:
[Tue Nov 17 11:20:17 2009] [notice] Parent: child process exited with status 3221225477 -- Restarting.
[Tue Nov 17 11:20:17 2009] [notice] Apache/2.2.4 (Win32) PHP/5.2.5 configured -- resuming normal operations
[Tue Nov 17 11:20:17 2009] [notice] Server built: Jan  9 2007 23:17:20
[Tue Nov 17 11:20:17 2009] [notice] Parent: Created child process 1872
[Tue Nov 17 11:20:18 2009] [notice] Child 1872: Child process is running
[Tue Nov 17 11:20:18 2009] [notice] Child 1872: Acquired the start mutex.
[Tue Nov 17 11:20:18 2009] [notice] Child 1872: Starting 250 worker threads.
[Tue Nov 17 11:20:18 2009] [notice] Child 1872: Starting thread to listen on port 80.
如果有高手知道这个,请告诉我,非常感谢

无论是什么情况下,通过测试已经知道,PHP的递归,效率确实比普通循环低很多.请斟酌后再用.
递归:
               
               
                ?php
set_time_limit(0);
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
function ads($i)
{
    if($i != 0)
    {
        $a = $i.'end
';
        $b = $i.'end
';
        $c = $i.'end
';
        $d = $i.'end
';
        ads($i-1);
    }
}
$stime=microtime_float();            //获取程序开始执行的时间
ads(22290);
$etime=microtime_float();            //获取程序执行结束的时间
$total=$etime-$stime;                //计算差值
echo "$total=$etime-$stime
";
echo "{$total} times
";
?>
非递归:
?php
set_time_limit(0);
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$cont = 22290;
$stime=microtime_float();            //获取程序开始执行的时间
for($i=0;$i$cont;$i++)
{
    $a .= $i.'end
';
    $b .= $i.'end
';
    $c .= $i.'end
';
    $d .= $i.'end
';
}
$etime=microtime_float();            //获取程序执行结束的时间
$total=$etime-$stime;                //计算差值
echo "$total=$etime-$stime
";
echo "{$total} times
";
?>
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105169/showart_2096547.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP