首頁  >  文章  >  後端開發  >  php Timer 頁面運行時間監測類

php Timer 頁面運行時間監測類

黄舟
黄舟原創
2017-02-17 10:14:261120瀏覽

php Timer 頁面運行時間監測類,可依不同key監測不同的運行時間



.

demo:


getKey($key);
        $this->_start[$flag] = $this->getMicrotime();
    }


    /** 记录结束时间
    * @param String $key 标记
    */
    public function end($key=''){
        $flag = $this->getKey($key);
        $this->_end[$flag] = $this->getMicrotime();
    }


    /** 计算运行时间
    * @param  String $key 标记
    * @return float
    */
    public function getTime($key=''){
        $flag = $this->getKey($key);
        if(isset($this->_end[$flag]) && isset($this->_start[$flag])){
            return (float)($this->_end[$flag] - $this->_start[$flag]);
        }else{
            return 0;
        }
    }


    /** 输出页面运行时间
    * @param  String $key 标记
    * @return String
    */
    public function printTime($key=''){
        printf("%srun time %f ms\r\n", $key==''? $key : $key.' ', $this->getTime($key)*1000);
    }


    /** 获取key
    * @param  String $key 标记
    * @return String 
    */
    private function getKey($key=''){
        if($key==''){
            return $this->_default_key;
        }else{
            return $this->_prefix.$key;
        }
    }


    /** 获取microtime
    */
    private function getMicrotime(){
        list($usec, $sec) = explode(' ', microtime());
        return (float)$usec + (float)$sec;
    }


} // class end

?>

demo運作輸出:


 以上就是php Timer 頁面運行時間監測類別的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn