<?php
set_time_limit(4);
echo ini_get('max_execution_time'); // give value as 4
while ($i<=10)
{
echo "i=$i ";
sleep(1);
$i++;
}
输出: 4i= i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
预期行为:它应该在 4 秒后结束执行。
请解释 ?
您使用的是哪个版本?
我使用了PHP 版本 5.4.7并得到了结果
4i=0 i=1 i=2 i=3 i=4
Fatal error: Maximum execution time of 4 seconds exceeded in .......
同样在设置set_time_limit()时, sleep()的持续时间将在execution time中被忽略。下图说明:
<?php
set_time_limit(20);
while ($i<=10)
{
echo "i=$i ";
sleep(100);
$i++;
}
?>
Output:
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
来源来自set_time_limit()
你应该试试这个,只要有一个脚本的睡眠时间超过你的最大执行时间。
sleep(ini_get('max_execution_time') + 10);
阅读:在 Linux 下,睡眠时间被忽略,但在 Windows 下,它算作执行时间。
这就是您获得计算时间和系统调用时间的方式。
// Script start
$rustart = getrusage();
// Code ...
// Script end
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls\n";