如何查找 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