在unixoid系统上(以及在Windows上的php 7+中),您可以使用getrusage,例如:
// start
$rustart = getrusage();
// Code ...
// end
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 \"This process used \" . rutime($ru, $rustart, \"utime\") .
\" ms for its computations\\n\";
echo \"It spent \" . rutime($ru, $rustart, \"stime\") .
\" ms in system calls\\n\";
请注意,如果要为每个测试生成一个php实例,则无需计算差异。
如果您只需要挂钟时间而不是CPU执行时间,那么计算起来很简单:
//place this before any you want to calculate time
$time_start = microtime(true);
//sample
for($i=0; $i<1000; $i++){
//do anything
}
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;
//execution time of the
echo \'<b>Total Execution Time:</b> \'.$execution_time.\' Mins\';
// if you get weird results, use number_format((float) $execution_time, 10)
请注意,这将包括PHP等待外部资源(如磁盘或数据库)的时间,这些资源不用于max_execution_time。
最简单的方法:
<?php
$time1 = microtime(true);
// code
//...
$time2 = microtime(true);
echo \' execution time: \' . ($time2 - $time1); //value in seconds 继续阅读与本文标签相同的文章
上一篇 :
php向字符串的指定位置插入另一个字符串
-
“缺芯少屏”现状或将改善,静电消除及洁净室工程需求增长
2026-05-15栏目: 教程
-
得助智能:企业如何利用数据做运营
2026-05-15栏目: 教程
-
“护网杯”2019年网络安全防护赛暨第二届工业互联网安全大赛圆满结束
2026-05-15栏目: 教程
-
来了,来了,它真的来了,12米超大幅面金属激光切割机出货了!
2026-05-15栏目: 教程
-
你的脸还要吗?机器人公司全球买人脸肖像权,报酬高达百万
2026-05-15栏目: 教程
