您好,欢迎访问一九零五行业门户网

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

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教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息