时时商务社区
标题:
php实现的Timer页面运行时间监测类
[打印本页]
作者:
qz234
时间:
2018-2-14 05:54
本文实例讲述了php实现的Timer页面运行时间监测类及其用法,是一款非常实用的PHP类文件。分享给大家供大家参考。具体分析如下:
该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程序设计有所帮助。
您可能感兴趣的文章:
php 页面执行时间计算代码
PHP 一个页面执行时间类代码
获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)
解析PHP计算页面执行时间的实现代码
计算php页面运行时间的函数介绍
php简单计算页面加载时间的方法
php设置页面超时时间解决方法
PHP记录页面停留时间的方法
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2