PHP Code Performance Analysis method

Source: Internet
Author: User

1. Functions used

microtime ()  , the function returns the current UNIX timestamp and the number of microseconds, this function returns a string in the format "msec sec", where the SEC is from the Unix era (0:00:00 January 1, 1970 GMT) to the present In the number of seconds, msec is the microsecond part. The two parts of a string are returned in seconds.

memory_get_usage ()  , function returns memory usage, can also have a parameter,$real _usageTRUEFALSE, will be Emalloc () The amount of memory used in the report is in byte (s), and the function needs to run on Linux.

Other related functions:

unset   Clear memory consumption of variables
mysql_free_result ($result)    $result $result mysql_query ($sql,$con);
memory_get_peak_usage ()        function returns peak memory usage, function needs to run on Linux
getrusage ()  Return cup usage, function needs to run on Linux    

2. Instance Code

Converts the memory usage of byte units returned by the Memory_get_usage () function to M, which is not used in this example

function memory_usage () {

$memory = (! function_exists (' memory_get_usage '))? ' 0 ': Round (Memory_get_usage ()/1024/1024, 2). ' MB ';

return $memory;

}


Get the exact timestamp plus the subtlety
functionmicrotime_float () {List($usec,$sec) =Explode(" ",Microtime()); return((float)$usec+ (float)$sec);}classt{Static $start _time; Static $end _time; Static $start _memory; Static $end _memory; Public Static functionstart () { self::$start _memory= Memory_get_usage ();//Unit is byte (s) Self::$start _time=microtime_float (); Echo' <br/>start @ '. Self::$start _time.‘ ('. Self::$start _memory. ') | -------> '; } Public Static function End() { self::$end _time=microtime_float (); Self::$end _memory=Memory_get_usage (); Echo' End @ '. Self::$end _time.‘ ('. Self::$end _memory.‘) :‘; Echo' |======= a total time: '. (Self::$end _time-self::$start _time). ', Shared memory: '. (Self::$end _memory-self::$start _memory); }}


Eliminate the impact of the first load of T class
T::start ();
T::end ();

T::start ();
$str = "I came to your city to walk the way you came, imagining how lonely you are without my days";
T::end ();

Show Results:

Start @1447408386.0921 (242528) |------->end @1447408386.0922 (242720): |======= total time: 3.6001205444336E-5, shared Memory: 192
Start @1447408386.0922 (242720) |------->end @1447408386.0922 (242856): |======= total time: 5.0067901611328E-6, shared memory: 136

PHP Code Performance Analysis method

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.