微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php分享几个跟踪脚本执行时间的函数

在unixoid系统上(以及在Windows上的PHP 7+中),您可以使用getrusage,例如:

// Script start

$rustart = getrusage();

// Code ...

// Script 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 computationsn";

echo "It spent " . rutime($ru,"stime") .

" ms in system callsn";

请注意,如果要为每个测试生成一个PHP实例,则无需计算差异。

如果您只需要挂钟时间而不是cpu执行时间,那么计算起来很简单:

//place this before any script you want to calculate time

$time_start = microtime(true);

//sample script

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 script

echo 'Total Execution Time: '.$execution_time.' Mins';

// if you get weird results,use number_format((float) $execution_time,10)

请注意,这将包括PHP等待外部资源(如磁盘或数据库)的时间,这些资源不用于max_execution_time。

最简单的方法

PHP

$time1 = microtime(true);

//script code

//...

$time2 = microtime(true);

echo 'script execution time: ' . ($time2 - $time1); //value in seconds

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐