**php Timer 頁面運行時間監測類,可按不同key監測不同的運行時間**
**Timer.class.php**
~~~
<?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:**
~~~
<?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運行輸出:**
~~~
program1 run time 163.285971 ms
program2 run time 100.347042 ms
run time 264.035940 ms
~~~
**源碼下載地址:[點擊查看](http://download.csdn.net/detail/fdipzone/6976601)**
- 前言
- php Captcha 驗證碼類
- php 替換敏感字符串
- php返回數據格式化類
- php XML文件解釋類
- php CSS Update Class
- PHPMailer - PHP email transport class
- PHP 遍歷文件夾及文件類及處理類
- 自動登入google play下載app report
- php click captcha 驗證碼類
- php 獲取頁面中的指定內容類
- php 支持斷點續傳的文件下載類
- php 縮略圖生成類,支持imagemagick及gd庫兩種處理
- php 根據url自動生成縮略圖
- php 過濾html標記屬性類
- php HTTP請求類,支持GET,POST,Multipart/form-data
- php Cookies 操作類
- php 密碼生成類
- php main 與 iframe 相互通訊類(同域/跨域)
- php 根據url自動生成縮略圖,并處理高并發問題
- php Timer 頁面運行時間監測類
- php 雙向隊列類
- php 導出CSV抽象類
- php zip文件內容比較類
- php 獲取/設置用戶訪問頁面語言類
- php 獲取Youtube某個User所有Video信息
- php 字符編碼轉換類,支持ANSI、Unicode、Unicode big endian、UTF-8、UTF-8+Bom 互相轉換
- php 版本處理類