测量php脚本执行时间的准确方法

测量php脚本执行时间的准确方法,php,Php,我想知道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 您可以使用该函数进行此操作。发件人: microtime-以微秒返回当前Unix时间戳 如果ge

我想知道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
您可以使用该函数进行此操作。发件人:

microtime
-以微秒返回当前Unix时间戳


如果
get\u as\u float
设置为
TRUE
,则
microtime()
返回一个float,它表示自Unix历元以来的当前时间(秒),精确到最近的微秒

用法示例:

$start = microtime(true);
while (...) {

}
$time_elapsed_secs = microtime(true) - $start;
请参阅。

$start=microtime(真);
对于($i=0;$i<10000;++$i){
//做点什么
}
$total=微时间(真)-$start;
echo$总计;

您的想法是正确的,只是该功能提供了更精确的计时

// ampersand is important thing here
function microSec( & $ms ) {
    if (\floatval( $ms ) == 0) {
        $ms = microtime( true );
    }
    else {
        $originalMs = $ms;
        $ms = 0;
        return microtime( true ) - $originalMs;
    }
}

// you don't have to define $ms variable. just function needs
// it to calculate the difference.
microSec($ms);
sleep(10);
echo microSec($ms) . " seconds"; // 10 seconds

for( $i = 0; $i < 10; $i++) {
    // you can use same variable everytime without assign a value
    microSec($ms);
    sleep(1);
    echo microSec($ms) . " seconds"; // 1 second
}

for( $i = 0; $i < 10; $i++) {
    // also you can use temp or useless variables
    microSec($xyzabc);
    sleep(1);
    echo microSec($xyzabc) . " seconds"; // 1 second
}

如果循环内的速度很快,则表观运行时间可能为零。如果是这样,请在代码周围环绕另一个循环并重复调用它。请确保将差值除以迭代次数,以获得一次a。我分析了需要10000000次迭代才能获得一致、可靠的计时结果的代码。

创建文件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";
    }
}

您可以通过以下方式使用
微时间(true)

将其放在php文件的开头:

//place this before any script you want to calculate time
$time_start = microtime(true);
// Display Script End time
$time_end = microtime(true);

//dividing with 60 will give the execution time in minutes other wise seconds
$execution_time = ($time_end - $time_start)/60;

//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
//你的脚本代码在这里

// do something
将其放在php文件的末尾:

//place this before any script you want to calculate time
$time_start = microtime(true);
// Display Script End time
$time_end = microtime(true);

//dividing with 60 will give the execution time in minutes other wise seconds
$execution_time = ($time_end - $time_start)/60;

//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
//显示脚本结束时间
$time\U end=微时间(真);
//除以60将给出以分钟或秒为单位的执行时间
$execution\u time=($time\u end-$time\u start)/60;
//脚本的执行时间
回显“总执行时间:”.$Execution_Time.“分钟”;

它将在
分钟内输出您的结果

您可以使用
服务器上的
请求时间
超全局数组:

请求时间

请求开始的时间戳。(从PHP 5.1.0开始提供。)

REQUEST\u TIME\u FLOAT

请求开始的时间戳,精度微秒。(从PHP 5.4.0开始提供。)

这样,您就不需要在脚本开头保存时间戳。您可以简单地执行以下操作:

<?php
// Do stuff
usleep(mt_rand(100, 10000));

// At the end of your script
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

echo "Did stuff in $time seconds\n";
?>

这里,
$time
将以秒为单位包含脚本启动以来经过的时间,精度为微秒(例如
1.341
1秒和341微秒)


更多信息:
PHP文档:和

这里有一个函数,它对任何一段PHP代码的执行进行计时,就像Python的timeit模块所做的那样:

如何使用它:

include('timeit.php');
const SOME_CODE = '
        strlen("foo bar");
';
$t = timeit(SOME_CODE);
print "$t[0] loops; $t[2] per loop\n";
结果:

$ php x.php 
100000 loops; 18.08us per loop
免责声明:我是本要点的作者


编辑:时间它现在是一个独立的、独立的项目,在这里有一个非常简单和简短的方法

<?php
$time_start = microtime(true);
//the loop begin
//some code
//the loop end
$time_end = microtime(true);
$total_time = $time_end - $time_start;
echo $total_time; // or whatever u want to do with the time
?>

下面是一个返回小数秒(即1.321秒)的实现


我想我会和大家分享我的功能。希望它能帮你节省时间

它最初用于跟踪基于文本的脚本的计时,因此输出为文本形式。但是,如果愿意,您可以轻松地将其修改为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)
}

希望这对别人有帮助

如果要以秒为单位显示该时间:

<?php

class debugTimer
{
    private $startTime;
    private $callsCounter;

    function __construct()
    {
        $this->startTime = microtime(true);
        $this->callsCounter = 0;
    }

    public function getTimer(): float
    {
        $timeEnd = microtime(true);
        $time = $timeEnd - $this->startTime;
        $this->callsCounter++;
        return $time;
    }

    public function getCallsNumber(): int
    {
        return $this->callsCounter;
    }
}

$timer = new debugTimer();
usleep(100);
echo '<br />\n
    ' . $timer->getTimer() . ' seconds before call #' . $timer->getCallsNumber();

usleep(100);
echo '<br />\n
    ' . $timer->getTimer() . ' seconds before call #' . $timer->getCallsNumber();

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


您可以使用单个函数以秒为单位找到执行时间

// ampersand is important thing here
function microSec( & $ms ) {
    if (\floatval( $ms ) == 0) {
        $ms = microtime( true );
    }
    else {
        $originalMs = $ms;
        $ms = 0;
        return microtime( true ) - $originalMs;
    }
}

// you don't have to define $ms variable. just function needs
// it to calculate the difference.
microSec($ms);
sleep(10);
echo microSec($ms) . " seconds"; // 10 seconds

for( $i = 0; $i < 10; $i++) {
    // you can use same variable everytime without assign a value
    microSec($ms);
    sleep(1);
    echo microSec($ms) . " seconds"; // 1 second
}

for( $i = 0; $i < 10; $i++) {
    // also you can use temp or useless variables
    microSec($xyzabc);
    sleep(1);
    echo microSec($xyzabc) . " seconds"; // 1 second
}
//符号在这里很重要
功能微秒(&$ms){
如果(\floatval($ms)==0){
$ms=微时间(真);
}
否则{
$originalMs=$ms;
$ms=0;
返回微时间(true)-$originalMs;
}
}
//您不必定义$ms变量。只是功能需要
//它需要计算差异。
微秒(毫秒);
睡眠(10);
echo microSec($ms)。“秒”;//10秒
对于($i=0;$i<10;$i++){
//您可以每次使用相同的变量,而无需赋值
微秒(毫秒);
睡眠(1);
回声微秒($ms)。“秒”//1秒
}
对于($i=0;$i<10;$i++){
//也可以使用临时变量或无用变量
microSec($xyzabc);
睡眠(1);
回声微秒($xyzabc)。“秒”//1秒
}

如果你想用返回值进行计算,你需要
微时间(true)
。我知道这太晚了(将近4年),但作为一个注释。。。根据PHP文档,使用这些计算(使用参数
get_as_float
as
true
)将以秒为单位给出结果。@patrick,这就是我所说的:如果
get_as_float
true
microtime()
返回代表秒数的值……这是一篇非常古老的文章,我们应该参考一个事实,返回的浮点值确实包含微秒。。。最好的解决办法是将其乘以1000。。。例如,这个答案已经过时了。现在,报告脚本执行时间的最佳方法是在代码末尾的一行:
$time=microtime(true)-$\u SERVER[“REQUEST\u time\u FLOAT”](答案中有更多信息。)如果在生产中需要,您可以随意使用microtime()语句,但如果只是为了测试,请举例说明。没有凌乱的代码是一个真正的加号。整洁!但是如果您没有显式销毁对象,则输出将出现在关闭标记之后,该标记是invalid@gondo不,它将是秒(微秒表示为秒的十进制值),这很容易知道。所以您可以使用:
$start=$\u SERVER[“请求时间”_