足球游戏_中国足彩网¥体育资讯$

php实现的Timer页面运行时间监测类
来源:易贤网 阅读:832 次 日期:2014-09-29 12:05:48
温馨提示:易贤网小编为您整理了“php实现的Timer页面运行时间监测类”,方便广大网友查阅!

本文实例讲述了php实现的Timer页面运行时间监测类及其用法,是一款非常实用的PHP类文件。分享给大家供大家参考。具体分析如下:

该php Timer页面运行时间监测类,可按不同key监测不同的运行时间。

Timer.class.php类文件如下:

?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 <?php /** Timer class, 计算页面运行时间,可按不同key计算不同的运行时间 * Date: 2014-02-28 * Author: fdipzone * Ver: 1.0 * * Func: * public start 记录开始时间 * public end 记录结束时间 * public getTime 计算运行时间 * pulbic printTime 输出运行时间 * private getKey 获取key * private getMicrotime 获取microtime */ class Timer{ // class start private $_start = array(); private $_end = array(); private $_default_key = 'Timer'; private $_prefix = 'Timer_'; /** 记录开始时间 * @param String $key 标记 */ public function start($key=''){ $flag = $this->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示例代码如下:

?123456789101112131415161718192021 <?php require 'Timer.class.php'; $timer = new Timer(); $timer->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运行输出:

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

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看网络编程
易贤网手机网站地址:php实现的Timer页面运行时间监测类
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标