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 computations\n";

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

" ms in system calls\n";

请注意,如果要为每个测试生成一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值