PHP Timer page run time monitoring class

Source: Internet
Author: User
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)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.