【问题标题】:How does php calculate execution time?php如何计算执行时间?
【发布时间】:2013-04-30 22:16:52
【问题描述】:

我正在使用云托管。 php脚本运行时间不同垂直比例设置:

  • 10 秒:128MB - 200Mhz
  • 1s : 2Gb - 3 Ghz

但是我感觉这两种情况的执行时间都小于1s,因为我设置了_time_limit(1),它没有显示超时错误。

可以肯定的是,我使用 getrusage() 来计算执行时间。在这两种情况下,它们都小于 1s。

这是什么行为?为什么第一个设置执行1s任务需要10s而不显示超时错误?

<?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>";
?>

【问题讨论】:

    标签: php timeout cloud cloud-hosting execution-time


    【解决方案1】:

    在我看来,您在 rutime() 中计算错误。

    为什么不使用更简单的方法来测量执行时间,如下所示:

    $start = microtime(true);
    
    //your stuff here
    
    $end = microtime(true);
    $finish = $end - $start;
    echo $finish;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2021-06-21
      相关资源
      最近更新 更多