php計算函數運行時間的類

<?php

/*
 *計算程序運行時間的類
 * 單位:毫秒
 * 調用方案:
 * 程序開頭:$time = new time(); $time->start();
 * 程序結尾:$time->stop();
 * echo "程序運行時間爲:".$time->spent();
 * */

class Time
{
    private $StartTime = 0;//程序運行開始時間
    private $StopTime = 0;//程序運行結束時間
    private $TimeSpent = 0;//程序運行花費時間

    function start()
    {
        //程序運行開始
        $this->StartTime = microtime(true);
    }

    function stop()
    {
        //程序運行結束
        $this->StopTime = microtime(true);
    }

    function spent()
    {
        //程序運行花費的時間
        $this->TimeSpent = $this->StopTime - $this->StartTime;
        //返回獲取到的程序運行時間差
        return number_format($this->TimeSpent * 1000, 4) . '毫秒';
    }
}

?>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章