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

Dom*_*oSL 251 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
Run Code Online (Sandbox Code Playgroud)

Tim*_*per 486

您可以使用此microtime功能.从文档:

microtime - 返回当前的Unix时间戳,以微秒为单位


如果get_as_float设置为TRUE,则microtime()返回一个浮点数,表示自Unix纪元精确到最接近的微秒以来的当前时间(以秒为单位).

用法示例:

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

}
$time_elapsed_secs = microtime(true) - $start;
Run Code Online (Sandbox Code Playgroud)

  • 如果你想用返回值进行计算,你需要`microtime(true)`. (36认同)
  • 我知道这是太晚了(差不多4年),但作为评论...根据PHP文档,使用这些计算(参数`get_as_float`为`true`)将给你**秒**的结果. (7认同)
  • “对于性能测量,建议使用 hrtime()。” https://www.php.net/manual/en/function.hrtime.php(PHP 7 >= 7.3.0,PHP 8) (7认同)
  • @patrick,这就是我说的:如果`get_as_float`是'true`,`microtime()`返回表示秒的值... (6认同)
  • 虽然这是一个非常古老的文章,但我们应该参考这样一个事实,即返回的浮点数确实包含微秒。最好的解决方案是将其乘以1000。以毫秒为单位获取当前时间(以毫秒为单位)。 (2认同)
  • 这个答案已经过时了。现在,报告脚本执行时间的最佳方法是在代码末尾添加一行:`$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];`(更多信息见[下面](https: //stackoverflow.com/a/19425158/8112776)) (2认同)

Adi*_*att 80

您可以使用microtime(true)以下方式:

把它放在你的php文件的开头:

//place this before any script you want to calculate time
$time_start = microtime(true);
Run Code Online (Sandbox Code Playgroud)

//你的脚本代码就在这里

// do something
Run Code Online (Sandbox Code Playgroud)

把它放在你的php文件的末尾:

// 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';
Run Code Online (Sandbox Code Playgroud)

它会输出你的结果minutes.


Iwa*_*aru 68

您可以使用REQUEST_TIME$_SERVER超全局数组.从文档:

'REQUEST_TIME'
请求开始的时间戳.从PHP 5.1.0开始提供.

'REQUEST_TIME_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";
?>
Run Code Online (Sandbox Code Playgroud)

这里,REQUEST_TIME将包含自脚本开始以来经过的时间(以秒为单位),精确度REQUEST_TIME_FLOAT为微秒(例如1秒和341微秒)

  • 你们混淆了微秒和毫秒。 (4认同)
  • @WimDeblauwe澄清一下,是的结果是在几秒钟内.但是用微秒_precision_.例如`1.1`等于1秒+100微秒. (3认同)
  • 这是衡量响应时间的最佳方法 (2认同)

Sol*_*ili 27

创建文件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";
    }
}
Run Code Online (Sandbox Code Playgroud)

<?php写完之后,比你脚本的开始include 'loadtime.php'; $loadtime=new loadTime();

当页面在最后加载时,将写入"在x秒内加载"

  • 整齐!但是如果你没有明确地销毁你的对象,输出将在关闭`</ html>`标签之后出现,该标签无效 (5认同)

Waq*_*leh 26

我这样做的方法是使用hrtime,它是为性能指标创建的,并且独立于系统时间。hrtime不受系统时间变化的影响。hrtime自 PHP 7.3.0 起可用

$start = hrtime(true);
sleep(5); // do something, in your case a loop
$end = hrtime(true);
$eta = $end - $start;
// convert nanoseconds to milliseconds
$eta /= 1e+6;
echo "Code block was running for $eta milliseconds";
Run Code Online (Sandbox Code Playgroud)

输出:

Code block was running for 5000.495206 milliseconds


Dvi*_*lay 18

$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
    // do something
}
$total = microtime(true) - $start;
echo $total;
Run Code Online (Sandbox Code Playgroud)


fla*_*ovs 7

这是一个函数,可以执行任何PHP代码,就像Python的timeit模块一样:https://gist.github.com/flaviovs/35aab0e85852e548a60a

如何使用它:

include('timeit.php');
const SOME_CODE = '
        strlen("foo bar");
';
$t = timeit(SOME_CODE);
print "$t[0] loops; $t[2] per loop\n";
Run Code Online (Sandbox Code Playgroud)

结果:

$ php x.php 
100000 loops; 18.08us per loop
Run Code Online (Sandbox Code Playgroud)

免责声明:我是这个要点的作者

编辑:timeit现在是一个独立的,自包含的项目,网址是https://github.com/flaviovs/timeit


wal*_*lyk 5

你有正确的想法,除了microtime()函数有更精确的时间.

如果循环内部的内容很快,则表观经过的时间可能为零.如果是这样,请在代码周围再换一个循环并重复调用它.务必将差异除以迭代次数以获得每次一次.我有配置代码,需要10,000,000次迭代才能获得一致,可靠的时序结果.