2011-06-05 27 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); 
    } 
}