php如何计算执行时间?
我正在使用云主机。php脚本运行时间因垂直比例设置的不同而不同:php如何计算执行时间?,php,timeout,cloud,cloud-hosting,execution-time,Php,Timeout,Cloud,Cloud Hosting,Execution Time,我正在使用云主机。php脚本运行时间因垂直比例设置的不同而不同: 10s:128MB-200Mhz 1s:2Gb-3GHz 但我觉得这两种情况下的执行时间都小于1s,因为我设置了时间限制(1),并且没有显示超时错误 当然,我使用getrusage()来计算执行时间。在这两种情况下,它们都小于1s 这是什么行为?为什么第一个设置需要10秒才能执行1s任务而不显示超时错误 <?php set_time_limit(1); $rustart = getrusage(); echo date(
- 10s:128MB-200Mhz
- 1s:2Gb-3GHz
<?php
set_time_limit(1);
$rustart = getrusage();
echo date("m/d/Y h:i:s a", time()).'<br>';
//Begin of the task
$a = 0;
for ($i=0; $i<6000000; $i++) {
$a = $a + $i;
if ($i % 1000000 == 0){
echo "$i <br>";
}
}
//End of the task
echo date("m/d/Y H:i:s a", time());
echo '<br>';
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
;
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations<br>";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls<br>";
?>
在我看来,您在rutime()中似乎算错了什么 为什么不使用更简单的方法来测量执行时间,如下所示:
$start = microtime(true);
//your stuff here
$end = microtime(true);
$finish = $end - $start;
echo $finish;