2011-06-05 59 views
181

我想知道环一个PHP需要多少毫秒执行。衡量PHP脚本的执行时间准确地

我知道一个通​​用算法的结构,但不知道如何实现它在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 
+0

可以摆弄)microtime中的大片(报表,如果您需要在此生产,但如果它只是用于测试,只需使用[Xdebug的的分析器(http://www.xdebug.org/docs/profiler)。没有混乱的代码是一个真正的优点。 – Wrikken 2011-06-05 21:39:39

回答

358

可以使用microtime功能这一点。从the documentation

microtime - 返回微秒电流Unix时间戳


如果get_as_float设置为TRUE,然后microtime()返回float,它代表以秒为单位的当前时间自Unix纪元精确到最近的微秒。

实例:

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

} 
$time_elapsed_secs = microtime(true) - $start; 
+29

如果你想用返回值进行计算,你需要'microtime(true)'。 – lonesomeday 2011-06-05 21:29:37

+6

我知道这是waaaaaay太晚了(差不多4年),但作为一个评论...使用这些计算(使用参数'get_as_float'为'true')会给你** **结果**,根据PHP文档。 – 2015-03-31 02:34:09

+0

@AlejandroIván:不,它不。 get_as_float将返回一个FLOAT而不是一个STRING,所以你可以用返回值进行计算......检查http://php.net/manual/en/function.microtime.php:如果get_as_float设置为TRUE,那么microtime()返回一个浮点数,它表示自Unix时期以来的秒数,精确到最近的微秒。 – patrick 2015-07-06 15:13:52

17
$start = microtime(true); 
for ($i = 0; $i < 10000; ++$i) { 
    // do something 
} 
$total = microtime(true) - $start; 
echo $total; 
5

你有正确的想法,但更精确的时序是可用的microtime()功能。

如果里面是什么循环快,有可能是明显的运行时间将为零。如果是这样,围绕代码包装另一个循环,并重复调用它。确保将差异除以迭代次数以获得每次一次的时间。我有需要10,000,000次迭代的简介代码才能获得一致,可靠的计时结果。

23

在脚本的beggining创建文件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"; 
    } 
} 

比,后<?phpinclude 'loadtime.php'; $loadtime=new loadTime();

当页面是在年底将有写“在x秒加载”

+4

整洁!但是,如果你没有明确地销毁你的对象,输出将出现在关闭''标签后面,该标签是无效的 – 2013-03-31 21:39:06

+0

@gondo不,这将是秒(以微秒表示为十进制值的情况) – FrancescoMM 2017-11-10 12:03:29

65

可以使用microtime(true)有以下几种方式:

在开始将这个你PHP文件:

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

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

// do something 

将这个在你的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'; 

它将输出你造成minutes

53

自PHP 5.4,你可以使用$_SERVER["REQUEST_TIME_FLOAT"]。它包含微秒级精度的请求开始的时间戳。

$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]; 

来源:http://php.net/manual/en/function.microtime.php

这可以让你测量只有一行的代码,例如脚本执行将其放在脚本的末尾。

+0

这是方便知道。所以你可以使用:'$ start = $ _SERVER [“REQUEST_TIME_FLOAT”]; $ myscript = microtime(true); $ bootstrap = $ myscript - $ start; ...做东西 ...; $ myscripttime = microtime(true) - $ myscript;' – pspahn 2015-02-24 22:01:37

+1

关于使用这个的完整点是,你可以这样做:'$ myscripttime = microtime(true) - $ _SERVER [“REQUEST_TIME_FLOAT”];'在你的结尾脚本,而不必在开始时保存时间戳。 – Iwazaru 2015-06-09 08:42:05

+1

根据链接文档,'$ time'将包含_seconds_中的差异,而不是_microseconds_中的差异。 – 2016-06-15 12:12:10

0

一个独立的,自包含的项目在这里是非常简单和短期的方法

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

这里的返回小数秒的实现(即1.321秒)

/** 
* MICROSECOND STOPWATCH FOR PHP 
* 
* Class FnxStopwatch 
*/ 
class FnxStopwatch 
{ 
    /** @var float */ 
    private $start, 
      $stop; 

    public function start() 
    { 
     $this->start = self::microtime_float(); 
    } 
    public function stop() 
    { 
     $this->stop = self::microtime_float(); 
    } 
    public function getIntervalSeconds() : float 
    { 
     // NOT STARTED 
     if (empty($this->start)) 
      return 0; 
     // NOT STOPPED 
     if (empty($this->stop)) 
      return ($this->stop - self::microtime_float()); 

     return $interval = $this->stop - $this->start; 
    } 

    /** 
    * FOR MORE INFO SEE http://us.php.net/microtime 
    * 
    * @return float 
    */ 
    private static function microtime_float() : float 
    { 
     list($usec, $sec) = explode(" ", microtime()); 

     return ((float)$usec + (float)$sec); 
    } 
}