Home > php教程 > php手册 > body text

php实现的Timer页面运行时间监测类,phptimer

WBOY
Release: 2016-06-13 09:25:00
Original
1038 people have browsed it

php实现的Timer页面运行时间监测类,phptimer

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

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

Timer.class.php类文件如下:

<&#63;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==''&#63; $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 
&#63;>

Copy after login

demo示例代码如下:

<&#63;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(); 
 
&#63;>

Copy after login

demo运行输出:

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

Copy after login

完整实例源码点击此处本站下载。

希望本文所述对大家的PHP程序设计有所帮助。

我想用php定时到:明天的00:00,怎实现?就是先获取当前时间,在当前时间起初上:0:0

首先
一楼回答有误,事实上php和网页完全无关。
“打开网页”这个操作仅仅就是给服务器发送信息,然后服务器执行php。
php可以直接在shell上作为脚本执行,也可以用网页启动后关闭网页照样执行。

然后回答楼主的问题

楼主你的要求,可是程序到达24:00这个时间则关闭?
那根本就不需要记录启动的时间,只要到达24点就执行关闭即可。
写一个死循环
ignore_user_abort(); // 使php可以在后台运行
set_time_limit(0); // 取消php的响应时间

do{
$time=date('h:i');//获得当前时间
if($time=="24:00")//到达时间则退出循环
break;
sleep(1000); // 每秒运行一次
}while(true);
?>
修改该程序即可达到你要的效果。如果你的要求是,一定要第二天24点才退出,那么请把date写成date('Y-M-D h:i')
然后使用split方法拆解计算日期
 

怎在PHP中使用计时器?

的确问得很不清楚。
你的出错原因是引用的文件不存在,这个类无法调用。
我的QQ 42696883
 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!