Micro-analysis of PHP performance
Performance analysis related functions and commands 1.1, time measurement functions usually we often use the Times () function, but the return is the number of seconds, for the internal performance analysis of a piece of code, to the second precision is not enough. So we use the Microtime function. The Microtime function can return two forms, one is the form of a string, and the other is the form of a floating-point number. However, it is important to note that, by default, the returned precision is only 4 decimal places. To achieve higher accuracy, we need to configure the precision. The following is the result of the use of microtime.
$start = Microtime (true); echo $start. " \ n "; $end = Microtime (true); echo $end. " \ n "; Echo ($end-$start). " \ n ";
The output is: bash-3.2# phptime.php 1441360050.3286 1441360050.3292 0.00053000450134277 and add a line to the front of the code:
Ini_set ("precision", 16); output: bash-3.2# phptime.php 1441360210.932628 1441360210.932831 0.0002031326293945312 except In addition to microtime internal statistics, you can also use Getrusage to get a long user state. In the actual operation, the time command is also used to calculate the duration of the whole program, run through multiple runs or modify the code, and get different lengths to get the difference in efficiency. The specific usage is: Time phptime.php, then after the completion of the program, regardless of whether or not to end the exit normally, there will be relevant statistics. bash-3.2# time phptime.php 1441360373.150756 1441360373.150959 0.0002031326293945312 real 0m0.186s user 0m0.072s sys 0m0. 077s because of the performance issues discussed in this article, we tend to analyze the gap and trend after millions of calls, in order to avoid some time-statistical code in the code, which we use later in the majority. 1.2, Memory use correlation function analysis Memory use function has two: Memory_ get_ usage, memory_ get_ peak_usage, the former can get the program at the point of call, that is, the memory currently used, The latter can get the memory used so far during peak times. The memory used is in bytes.
$base _memory= memory_get_usage (); echo "hello,world!\n"; $end _memory= memory_get_usage (); $peak _memory= memory_get_peak_usage (); echo $base _memory, "\ T", $end _memory, "\ T", ($end _memory-$base _memory), "\ T", $peak _memory, "\ n";
The output is as follows: bash-3.2# phphelloworld.php hello,world! 224400 224568 168 227424 As you can see, even if you only output a single word in the middle of the program, plus the variable storage, it consumes 168 bytes of memory. For the same program, different versions of PHP do not use the same memory, or even vary greatly.
$baseMemory = Memory_get_usage (); Class User { private $uid; function __construct ($uid) { $this->uid= $uid; } } for ($i =0; $i <100000; $i + +) { $obj = new User ($i); if ($i% 10000 = = = 0) { echo sprintf ('%6d: ', $i), Memory_get_usage (), "bytes\n"; } } echo " Peak:", Memory_get_peak_usage (True), "bytes\n";
In PHP 5.2, memory is used as follows:
[root@localhostphpperf]# php52 memory.php 0:93784 bytes 10000:93784 bytes ... 80000:93784 bytes 90000:93784 bytes peak:262144 bytes
In PHP 5.3, memory is used as follows
[root@localhostphpperf]# phpmemory.php 0:634992 bytes 10000:634992 bytes ... 80000:634992 bytes 90000:634992 bytes peak:786432 bytes
It can be seen that PHP 5.3 in memory use a bit more extensive. PHP 5.4-5.6 almost, optimized:
[root@localhostphpperf]# php56 memory.php 0:224944 bytes 10000:224920 bytes ... 80000:224920 bytes 90000:224920 bytes peak:262144 bytes
When PHP 7 is used in a small amount, the peak memory usage increases a lot.
[root@localhostphpperf]# php7 memory.php 0:353912 bytes 10000:353912 bytes ... 80000:353912 bytes 90000:353912 bytes peak:2097152 bytes
As seen above, the PHP used above has a better garbage collection mechanism, 100,000 initialization, and does not increase with the object initialization of the use of memory. PHP7 's peak memory is used up to close to 2M. Here is another example, based on the above code, we add a line, that is, the following bold line: $obj->self = $obj; The code is as follows:
$baseMemory = Memory_get_usage (); Class User { private $uid; function __construct ($uid) { $this->uid= $uid; } } for ($i =0; $i <100000; $i + +) { $obj = new User ($i); $obj->self = $obj; if ($i% = = = 0) { echo sprintf ('%6d: ', $i), Memory_get_usage (), "bytes\n"; } }
echo "Peak:", Memory_get_peak_usage (True), "bytes\n";
http://www.bkjia.com/PHPjc/1053349.html www.bkjia.com true http://www.bkjia.com/PHPjc/1053349.html techarticle micro-analysis of performance in PHP I. Performance analysis related functions and commands 1.1, time metric functions usually we often use times () function, but return the number of seconds, for a piece of code inside ...