zoukankan      html  css  js  c++  java
  • PHP Timer 页面运行时间监测类

    转至:http://blog.csdn.net/fdipzone/article/details/20160567

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

    Timer.class.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
    ", $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

    DOME:

    <?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();  
      
    ?> 
  • 相关阅读:
    hdu 1087(LIS变形)
    poj 1088(记忆化搜索)
    hdu 1505(最大子矩阵)
    hdu 1506(好题+DP或者RMQ)
    poj 2593&&poj2479(最大两子段和)
    hdu 1003(最大子段和)
    hdu 2881(LIS变形)
    poj 1692(动态规划)
    CodeForces 626C Block Towers
    CodeForces 626B Cards
  • 原文地址:https://www.cnblogs.com/bit5566/p/5143713.html
Copyright © 2011-2022 走看看