计算php代码执行时间长短的类(精确到毫秒)

/**
 * PHP脚本执行时间计算
 */
class runtime
{
    var $StartTime = 0;
    var $StopTime = 0;




    function get_microtime()
    {
        list($usec, $sec) = explode(' ', microtime());
//var_dump($usec);var_dump($sec);
        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));
        //毫秒
//$msec = $spent*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();


?>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部