当前位置:Gxlcms > PHP教程 > 关于phpTimer页面运行时间监测类的相关介绍

关于phpTimer页面运行时间监测类的相关介绍

时间:2021-07-01 10:21:17 帮助过:11人阅读

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

Timer.class.php

  1. <?php
  2. /** Timer class, 计算页面运行时间,可按不同key计算不同的运行时间
  3. * Date: 2014-02-28
  4. * Author: fdipzone
  5. * Ver: 1.0
  6. *
  7. * Func:
  8. * public start 记录开始时间
  9. * public end 记录结束时间
  10. * public getTime 计算运行时间
  11. * 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:

  1. <?php
  2. require 'Timer.class.php';
  3. $timer = new Timer();
  4. $timer->start();
  5. $timer->start('program1');
  6. usleep(mt_rand(100000,500000));
  7. $timer->end('program1');
  8. $timer->printTime('program1');
  9. $timer->start('program2');
  10. usleep(mt_rand(100000,500000));
  11. $timer->end('program2');
  12. $timer->printTime('program2');
  13. $timer->end();
  14. $timer->printTime();
  15. ?>

demo运行输出:

  1. program1 run time 163.285971 ms
  2. program2 run time 100.347042 ms
  3. run time 264.035940 ms

本篇文章讲解了关于php Timer 页面运行时间监测类相关内容,更多相关知识请关注Gxl网。

相关推荐:

关于php 构造函数支持不同个数参数的方法介绍

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

关于php 双向队列类的讲解

以上就是关于php Timer 页面运行时间监测类的相关介绍的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行