如何查詢 PHP 執行時間?
在 PHP 版本 7+ 中,可以使用 getrusage 函式。以下是一個示例程式碼演示 −
示例
//beginning of the script
$exec_start = getrusage();
//other code functionalities
//end of the script
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "The process used " . rutime($ru, $exec_start, "utime") .
" ms for the computations
";
echo "Spent " . rutime($ru, $exec_start, "stime") .
" ms during system calls
";注意 − 如果針對每個測試生成一個 php 例項,則無需計算時間差。
輸出
這將產生如下輸出 −
The process used 1.563896 ms for the computations Spent 0.345628 ms during system calls
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP