Home  >  Article  >  Backend Development  >  Introduction to the php Timer page running time monitoring class

Introduction to the php Timer page running time monitoring class

jacklove
jackloveOriginal
2018-06-09 14:11:231378browse

php Timer page running time monitoring class, which can monitor different running times according to different keys

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 running output:

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

This article explains the content related to php Timer page running time monitoring. For more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

About the method of php constructor supporting different numbers of parameters

How to use PDO to query mysql Methods to avoid SQL injection

Explanation on PHP two-way queue class

The above is the detailed content of Introduction to the php Timer page running time monitoring class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn