PHP Timer page run time monitoring class, can be different keys to monitor the different running time
Timer.class.php
<?php/** Timer class, calculates the page run time, can calculate different running time according to different key * date:2014-02-28* author:fdipzone* ver:1.0** func:* p Ublic start record Starting time * Public end record End time * Public getTime calculation run time * pulbic printtime output Run time * Private GetKey get key* private getmicrotime get Microtime*/class timer{//class start Private $_start = Array (); Private $_end = Array (); Private $_default_key = ' Timer '; Private $_prefix = ' timer_ '; /** Record Start time * @param String $key Tag */Public Function start ($key = ') {$flag = $this->getkey ($key); $this->_start[$flag] = $this->getmicrotime (); }/** record End time * @param String $key Tag */Public Function end ($key = ') {$flag = $this->getkey ($key); $this->_end[$flag] = $this->getmicrotime (); /** Calculation Run time * @param String $key tag * @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; }}/** Output page run time * @param string $key tag * @return String */Public Function printtime ($key = ') { printf ("%srun time%f ms\r\n", $key = = '? $key: $key. ' ', $this->gettime ($key) *1000); /** gets the key * @param string $key tag * @return String */Private Function GetKey ($key = ") {if ($key = = ') {return $this->_default_key; }else{return $this->_prefix. $key; }}/** gets microtime */Private Function Getmicrotime () {list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec; }}//Class end?>
Demo
<?phprequire ' 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 Run output:
Program1 run time 163.285971 msprogram2 run time 100.347042 msrun time 264.035940 ms
The above is the PHP Timer page Runtime monitoring class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!