Example of php statistics time and memory usage

Source: Internet
Author: User

Copy codeThe Code is as follows:
/**
* Recording and statistical time (microseconds) and memory usage
* Usage:
* <Code>
* G ('begin'); // The start flag of the record.
* // Run the code in the... Interval
* G ('end'); // The end tag of the record.
* Echo G ('begin', 'end', 6); // the running time of the statistical interval is accurate to the last six digits of the decimal number.
* Echo G ('begin', 'end', 'M'); // calculates the memory usage in the interval.
* If the end tag is not defined, the current tag is automatically used as the tag.
* For memory usage statistics, the MEMORY_LIMIT_ON constant must be set to true.
* </Code>
* @ Param string $ start tag
* @ Param string $ end tag
* @ Param integer | string $ dec decimal point or m
* @ Return mixed
*/
Function G ($ start, $ end = '', $ dec = 4 ){
Static $ _ info = array ();
Static $ _ mem = array ();
If (is_float ($ end) {// record the time
$ _ Info [$ start] = $ end;
} Elseif (! Empty ($ end) {// time and memory usage
If (! Isset ($ _ info [$ end]) $ _ info [$ end] = microtime (TRUE );
If (MEMORY_LIMIT_ON & $ dec = 'M '){
If (! Isset ($ _ mem [$ end]) $ _ mem [$ end] = memory_get_usage ();
Return number_format ($ _ mem [$ end]-$ _ mem [$ start])/1024 );
} Else {
Return number_format ($ _ info [$ end]-$ _ info [$ start]), $ dec );
}

} Else {// record time and memory usage
$ _ Info [$ start] = microtime (TRUE );
If (MEMORY_LIMIT_ON) $ _ mem [$ start] = memory_get_usage ();
}
}

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.