PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

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

原创
2016-07-29 08:54:58 801浏览

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{
$a *= $i;
}
$runtime->stop();
$spent_time = $runtime->spent($echo=true, '测试脚本');
$runtime->clear();

测试结果:

php,php脚本语言,脚本,php脚本执行时间,php shell脚本,php教程,php 脚本参数,php脚本执行,php脚本怎么运

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。