首頁  >  文章  >  後端開發  >  關於php Timer 頁面運行時間監測類別的相關介紹

關於php Timer 頁面運行時間監測類別的相關介紹

jacklove
jacklove原創
2018-06-09 14:11:231376瀏覽

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

Timer.class.php

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:

#
start();
$timer->start('program1');
usleep(mt_rand(100000,500000));
$timer->end('program1');
$timer->printTime('program1');
$timer->start('program2');
usleep(mt_rand(100000,500000));
$timer->end('program2');
$timer->printTime('program2');
$timer->end();
$timer->printTime();
?>

demo運行輸出:

program1 run time 163.285971 ms
program2 run time 100.347042 ms
run time 264.035940 ms

本篇文章講解了關於php Timer 頁面運行時間監測類相關內容,更多相關知識請關注php中文網。

相關推薦:

關於php 建構子支援不同個數參數的方法介紹

如何使用PDO查詢mysql避免SQL注入的方法

關於php 雙向佇列類別的講解

以上是關於php Timer 頁面運行時間監測類別的相關介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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