Chinaunix首页 | 论坛 | 博客
  • 博客访问: 245494
  • 博文数量: 76
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 745
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-28 16:04
文章分类

全部博文(76)

文章存档

2013年(2)

2010年(21)

2009年(53)

我的朋友

分类:

2009-11-17 11:03:49

纯属个人兴趣,比较了一下递归的效率
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
"
;
?>


阅读(1360) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~