为了账号安全,请及时绑定邮箱和手机立即绑定

测量php脚本执行时间的准确方法

测量php脚本执行时间的准确方法

PHP
ABOUTYOU 2019-10-15 09:11:06
我想知道一个PHP for循环执行需要多少毫秒。我知道通用算法的结构,但是不知道如何在PHP中实现它:Begininit1 = timer(); // where timer() is the amount of milliseconds from midnightthe loop beginsome codethe loop endtotal = timer() - init1;End
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

您可以使用此microtime功能。从文档中:


microtime —返回当前的Unix时间戳(以微秒为单位)


如果get_as_float将设置为TRUE,则microtime()返回一个浮点数,它表示自Unix纪元以来精确到最接近的微秒的当前时间(以秒为单位)。


用法示例:


$start = microtime(true);

while (...) {


}

$time_elapsed_secs = microtime(true) - $start;


查看完整回答
反对 回复 2019-10-15
?
繁花如伊

TA贡献2012条经验 获得超12个赞

您可以microtime(true)通过以下方式使用:


将其放在您的php文件的开头:


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

$time_start = microtime(true);

//您的脚本代码在这里


// do something

把它放在你的php文件的末尾:


// Display Script End time

$time_end = microtime(true);


//dividing with 60 will give the execution time in minutes other wise seconds

$execution_time = ($time_end - $time_start)/60;


//execution time of the script

echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';

它将输出结果minutes。


查看完整回答
反对 回复 2019-10-15
  • 3 回答
  • 0 关注
  • 691 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信