搜索
首页 电脑/网络

PHP计算脚本执行时间类php脚本参数php脚本执行php脚本怎么运

全部回答

2023-03-13

0 0
1.优化代码的时候,脚本的执行时间是一个很重要的考量方式,那么如何用PHP来实现计算PHP脚本的运行时间呢?

下面推荐给大家一个很好用得类.

runtime.class.php

/**
* PHP脚本执行时间计算
*/
class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
function start()
{
$this->StartTime = $this->get_microtime();
}
function stop()
{
$this->StopTime = $this->get_microtime();
}
function spent($echo=false,$title='')
{
$spent = sprintf('%.4f',round(($this->StopTime - $this->StartTime) * 1000, 1)/1000);
if($echo){
echo $title."执行时间:{$spent}秒
";
}else{
return $spent;
}
}
function clear()
{
$this->StartTime = 0;
$this->StopTime = 0;
}
}

测试代码:

#测试脚本代码
$runtime= new runtime;
$runtime->start();
$a = 0;
for($i=0; $i<100000; $i++)
{
$a *= $i;
}
$runtime->stop();
$spent_time = $runtime->spent($echo=true, '测试脚本');
$runtime->clear();

测试结果:

以上就介绍了PHP计算脚本执行时间类,包括了php,脚本方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
电脑/网络
程序设计
电脑装机
操作系统/系统故障
硬件
笔记本电脑
百度
互联网
反病毒
软件
举报
举报原因(必选):
取消确定举报