加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 02:31:29 所属栏目:PHP 来源:互联网
导读:本文章向大家介绍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。

最简单的方法:

$time1 = microtime(true);

//script code

//...

$time2 = microtime(true);

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读