Micro-analysis of PHP performance. Micro-analysis of PHP performance I. performance analysis-related functions and commands 1.1. we usually use time () functions, but return seconds, micro-analysis of the internal PHP performance of a code segment
I. Functions and commands related to performance analysis
1.1. time measurement functions
We usually use the time () function, but the returned number of seconds. for the internal performance analysis of a piece of code, the second precision is not enough. Therefore, use the microtime function. The microtime function can return two forms: string and floating point. However, it should be noted that by default, the returned precision is only four decimal places. To achieve higher accuracy, we need to configure precision.
The following is the use result of microtime.
$start= microtime(true); echo $start."\n"; $end = microtime(true); echo $end."\n"; echo ($end-$start)."\n";
Output:
Bash-3.2 # phptime. php
1441360050.3286
1441360050.3292
0.00053000450134277
Add a line before the code:
Ini_set ("precision", 16 );
Output:
Bash-3.2 # phptime. php
1441360210.932628
1441360210.932831
0.0002031326293945312
In addition to the internal statistics of microtime, you can also use getrusage to obtain the length of user state. In actual operations, the time command is also commonly used to calculate the running duration of the entire program. it can be run after multiple operations or code modifications, get different time lengths to get the difference in efficiency. The specific usage is: time phptime. php. after the program is run, statistics will be collected no matter whether the program ends normally or not.
Bash-3.2 # time phptime. php
1441360373.150756
1441360373.150959
0.0002031326293945312
Real 0m0. 186 s
User 0m0. 072 s
Sys 0m0. 077 s
Because of the performance issues discussed in this article, we often analyze the gaps and trends after hundreds of thousands of calls. to avoid some time statistics code in the code, we will mostly use the time command later.
1.2 Memory usage related functions
There are two functions used for memory analysis: memory _ get _ usage and memory _ get _ peak_usage. The former can obtain the time point at which the program is called, that is, the memory used currently, the latter can obtain the memory used during peak periods so far. 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
We can see that even if only one sentence is output in the middle of the program and the variable storage is added, 168 bytes of memory is consumed.
For the same program, different PHP versions have different or even different memory usage.
$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, the memory usage is 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, the memory usage is 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 is more extensive in memory usage.
PHP 5.4-5.6 is similar and 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 we can see from the above, the above PHP has a relatively good garbage collection mechanism, 0.1 million initialization, and does not increase the memory usage with the increase of object initialization. The peak memory usage of PHP 7 is the most, reaching nearly 2 M.
Next let's look at an 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% 5000 === 0 ) { echo sprintf( '%6d: ', $i), memory_get_usage(), " bytes\n"; } }
Echo "peak:", memory_get_peak_usage (true), "bytes \ n ";
Micro Analysis of http://www.bkjia.com/PHPjc/1053349.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1053349.htmlTechArticlePHP performance I. performance analysis related functions and commands 1.1, time measure function we usually use time () function, but the return is the number of seconds, for an internal code segment...