我想知道执行一个PHP for循环需要多少毫秒。

我知道一个泛型算法的结构,但不知道如何在PHP中实现它:

Begin
init1 = timer(); // where timer() is the amount of milliseconds from midnight
the loop begin
some code
the loop end
total = timer() - init1;
End

当前回答

我想分享一下我的功能。希望能帮你节省时间。

它最初用于跟踪基于文本的脚本的时间,因此输出是文本形式的。但如果您愿意,可以轻松地将其修改为HTML。

它将为您计算自脚本开始和在每个步骤中花费了多少时间。它将所有输出格式化为3个精度小数。(精确到毫秒。)

一旦将它复制到脚本的顶部,所要做的就是将recordTime函数调用放在想要计时的每个片段之后。

复制到你的脚本文件的顶部:

$tRecordStart = microtime(true);
header("Content-Type: text/plain");
recordTime("Start");

function recordTime ($sName) {
  global $tRecordStart;
  static $tStartQ;
  $tS = microtime(true);
  $tElapsedSecs = $tS - $tRecordStart;
  $tElapsedSecsQ = $tS - $tStartQ;
  $sElapsedSecs = str_pad(number_format($tElapsedSecs, 3), 10, " ", STR_PAD_LEFT);
  $sElapsedSecsQ = number_format($tElapsedSecsQ, 3);
  echo "//".$sElapsedSecs." - ".$sName;
  if (!empty($tStartQ)) echo " In ".$sElapsedSecsQ."s";
  echo "\n";
  $tStartQ = $tS;
}

想要记录时间的流逝,你可以这样做:

recordTime("What We Just Did")

例如:

recordTime("Something Else")
//Do really long operation.
recordTime("Really Long Operation")
//Do a short operation.
recordTime("A Short Operation")
//In a while loop.
for ($i = 0; $i < 300; $i ++) {
  recordTime("Loop Cycle ".$i)
}

给出如下输出:

//     0.000 - Start
//     0.001 - Something Else In 0.001s
//    10.779 - Really Long Operation In 10.778s
//    11.986 - A Short Operation In 1.207s
//    11.987 - Loop Cycle 0 In 0.001s
//    11.987 - Loop Cycle 1 In 0.000s
...
//    12.007 - Loop Cycle 299 In 0.000s

希望这能帮助到一些人!

其他回答

下面是我用来测量平均时间的脚本

<?php

$times = [];
$nbrOfLoops = 4;
for ($i = 0; $i < $nbrOfLoops; ++$i) {
    $start = microtime(true);
    sleep(1);
    $times[] = microtime(true) - $start;
}

echo 'Average: ' . (array_sum($times) / count($times)) . 'seconds';

我想分享一下我的功能。希望能帮你节省时间。

它最初用于跟踪基于文本的脚本的时间,因此输出是文本形式的。但如果您愿意,可以轻松地将其修改为HTML。

它将为您计算自脚本开始和在每个步骤中花费了多少时间。它将所有输出格式化为3个精度小数。(精确到毫秒。)

一旦将它复制到脚本的顶部,所要做的就是将recordTime函数调用放在想要计时的每个片段之后。

复制到你的脚本文件的顶部:

$tRecordStart = microtime(true);
header("Content-Type: text/plain");
recordTime("Start");

function recordTime ($sName) {
  global $tRecordStart;
  static $tStartQ;
  $tS = microtime(true);
  $tElapsedSecs = $tS - $tRecordStart;
  $tElapsedSecsQ = $tS - $tStartQ;
  $sElapsedSecs = str_pad(number_format($tElapsedSecs, 3), 10, " ", STR_PAD_LEFT);
  $sElapsedSecsQ = number_format($tElapsedSecsQ, 3);
  echo "//".$sElapsedSecs." - ".$sName;
  if (!empty($tStartQ)) echo " In ".$sElapsedSecsQ."s";
  echo "\n";
  $tStartQ = $tS;
}

想要记录时间的流逝,你可以这样做:

recordTime("What We Just Did")

例如:

recordTime("Something Else")
//Do really long operation.
recordTime("Really Long Operation")
//Do a short operation.
recordTime("A Short Operation")
//In a while loop.
for ($i = 0; $i < 300; $i ++) {
  recordTime("Loop Cycle ".$i)
}

给出如下输出:

//     0.000 - Start
//     0.001 - Something Else In 0.001s
//    10.779 - Really Long Operation In 10.778s
//    11.986 - A Short Operation In 1.207s
//    11.987 - Loop Cycle 0 In 0.001s
//    11.987 - Loop Cycle 1 In 0.000s
...
//    12.007 - Loop Cycle 299 In 0.000s

希望这能帮助到一些人!

这样会更清楚

<?php

$start = hrtime(true);

while (...) {
}

$duration = hrtime(true) - $start;
echo  $duration * 1000 ." -> microseconds". PHP_EOL; 
echo  $duration * 1000000 ." -> milliseconds". PHP_EOL; 
echo  $duration * 1e6 ." -> milliseconds". PHP_EOL; 

结果

37180000 -> microseconds
37180000000 -> milliseconds
37180000000 -> milliseconds

创建文件loadtime.php

<?php
class loadTime{
    private $time_start     =   0;
    private $time_end       =   0;
    private $time           =   0;
    public function __construct(){
        $this->time_start= microtime(true);
    }
    public function __destruct(){
        $this->time_end = microtime(true);
        $this->time = $this->time_end - $this->time_start;
        echo "Loaded in $this->time seconds\n";
    }
}

而不是在<?PHP: loadtime.php美元loadtime = new loadtime ();

当页面加载结束时,会写“加载在x秒内”