网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的范文:

 

标题 php实现的timer页面运行时间监测类
范文
    本文实例讲述了php实现的timer页面运行时间监测类及其用法,是一款非常实用的php类文件。分享给大家供大家参考。具体分析如下:
    该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 msrn, $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
    希望本文所述对大家的php程序设计有所帮助。
随便看

 

在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/15 8:28:10