登录  /  注册

php Timer 页面运行时间监测类

黄舟
发布: 2017-02-17 10:14:26
原创
1065人浏览过

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 = &#39;Timer&#39;;
    private $_prefix = &#39;Timer_&#39;;


    /** 记录开始时间
    * @param String $key 标记
    */
    public function start($key=&#39;&#39;){
        $flag = $this->getKey($key);
        $this->_start[$flag] = $this->getMicrotime();
    }


    /** 记录结束时间
    * @param String $key 标记
    */
    public function end($key=&#39;&#39;){
        $flag = $this->getKey($key);
        $this->_end[$flag] = $this->getMicrotime();
    }


    /** 计算运行时间
    * @param  String $key 标记
    * @return float
    */
    public function getTime($key=&#39;&#39;){
        $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=&#39;&#39;){
        printf("%srun time %f ms\r\n", $key==&#39;&#39;? $key : $key.&#39; &#39;, $this->getTime($key)*1000);
    }


    /** 获取key
    * @param  String $key 标记
    * @return String 
    */
    private function getKey($key=&#39;&#39;){
        if($key==&#39;&#39;){
            return $this->_default_key;
        }else{
            return $this->_prefix.$key;
        }
    }


    /** 获取microtime
    */
    private function getMicrotime(){
        list($usec, $sec) = explode(&#39; &#39;, microtime());
        return (float)$usec + (float)$sec;
    }


} // class end

?>
登录后复制

demo:

<?php

require &#39;Timer.class.php&#39;;

$timer = new Timer();
$timer->start();

$timer->start(&#39;program1&#39;);
usleep(mt_rand(100000,500000));
$timer->end(&#39;program1&#39;);
$timer->printTime(&#39;program1&#39;);

$timer->start(&#39;program2&#39;);
usleep(mt_rand(100000,500000));
$timer->end(&#39;program2&#39;);
$timer->printTime(&#39;program2&#39;);

$timer->end();
$timer->printTime();

?>
登录后复制

demo运行输出:

program1 run time 163.285971 ms
program2 run time 100.347042 ms
run time 264.035940 ms
登录后复制

 以上就是php Timer 页面运行时间监测类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号