PHP Performance Analysis and Experiment: Micro-Performance Analysis (1)

Source: Internet
Author: User
Tags types of functions

PHP Performance Analysis and Experiment: Micro-Performance Analysis (1)

In the previous article, we discussed the performance of PHP from three aspects: explanatory language, Dynamic Language, and underlying implementation. This article goes deep into the micro-level of PHP. We will learn about the performance of PHP in the process of using and writing code, which may require attention and improvement.

Before starting the analysis, we need to know some functions related to performance analysis. These functions allow us to better analyze and evaluate the program.

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.

 
 
  1. $start= microtime(true); 
  2. echo $start."/n"; 
  3. $end = microtime(true); 
  4. echo $end."/n"; 
  5. echo ($end-$start)."/n"; 

Output:

 
 
  1. bash-3.2# phptime.php 
  2.  
  3. 1441360050.3286  
  4. 1441360050.3292  
  5. 0.00053000450134277 

Add a line before the Code:

 
 
  1. ini_set("precision", 16); 

Output:

 
 
  1. bash-3.2# phptime.php 
  2. 1441360210.932628  
  3. 1441360210.932831  
  4. 0.0002031326293945312 

In addition to the internal statistics of microtime, you can also use getrusage to obtain the duration of the 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.

 
 
  1. bash-3.2# time phptime.php 
  2. 1441360373.150756  
  3. 1441360373.150959  
  4. 0.0002031326293945312 
  5. real 0m0.186s  
  6. user 0m0.072s  
  7. sys 0m0.077s 

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.

 
 
  1. $base_memory= memory_get_usage(); 
  2. echo "Hello,world!/n"; 
  3. $end_memory= memory_get_usage(); 
  4. $peak_memory= memory_get_peak_usage(); 
  5. echo $base_memory,"/t",$end_memory,"/t",($end_memory-$base_memory),"/t", $peak_memory,"/n"; 

The output is as follows:

 
 
  1. bash-3.2# phphelloworld.php 
  2. Hello,world!  
  3. 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.

 
 
  1. $baseMemory= memory_get_usage(); 
  2. class User 
  3. private $uid; 
  4. function __construct($uid) 
  5.     { 
  6. $this->uid= $uid; 
  7.     } 
  8.  
  9. for($i=0;$i<100000;$i++) 
  10. $obj= new User($i); 
  11. if ( $i% 10000 === 0 ) 
  12.     { 
  13. echo sprintf( '%6d: ', $i), memory_get_usage(), " bytes/n"; 
  14.     } 
  15. echo "  peak: ",memory_get_peak_usage(true), " bytes/n"; 

In PHP 5.2, the memory usage is as follows:

 
 
  1. [root@localhostphpperf]# php52 memory.php 
  2.  
  3. 0: 93784 bytes  
  4. 10000: 93784 bytes  
  5. …… 80000: 93784 bytes  
  6. 90000: 93784 bytes  
  7. peak: 262144 bytes 

In PHP 5.3, the memory usage is as follows:

 
 
  1. [root@localhostphpperf]# phpmemory.php 
  2.  
  3. 0: 634992 bytes  
  4. 10000: 634992 bytes  
  5. …… 80000: 634992 bytes  
  6. 90000: 634992 bytes  
  7. 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:

 
 
  1. [root@localhostphpperf]# php56 memory.php 
  2.  
  3. 0: 224944 bytes  
  4. 10000: 224920 bytes  
  5. …… 80000: 224920 bytes  
  6. 90000: 224920 bytes  
  7. peak: 262144 bytes 

When PHP 7 is used in a small amount, the peak memory usage increases a lot.

 
 
  1. [root@localhostphpperf]# php7 memory.php 
  2.  
  3. 0: 353912 bytes  
  4. 10000: 353912 bytes  
  5. …… 80000: 353912 bytes  
  6. 90000: 353912 bytes  
  7. 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, as shown below:

$ Obj-> self = $ obj;

The Code is as follows:

 
 
  1. $baseMemory= memory_get_usage(); 
  2. class User 
  3. private $uid; 
  4. function __construct($uid) 
  5.     { 
  6. $this->uid= $uid; 
  7.     } 
  8.  
  9. for($i=0;$i<100000;$i++) 
  10. $obj= new User($i); 
  11. $obj->self = $obj; 
  12. if ( $i% 5000 === 0 ) 
  13.     { 
  14. echo sprintf( '%6d: ', $i), memory_get_usage(), " bytes/n"; 
  15.     } 
  16. echo "  peak: ",memory_get_peak_usage(true), " bytes/n"; 

Now let's take a look at the memory usage. The main part of the intermediate table is the memory usage, in bytes.

The chart is as follows:

PHP 5.2 does not have an appropriate garbage collection mechanism, resulting in more and more memory usage. The memory recovery mechanism after 5.3 causes the memory to be stable in one interval. You can also see that PHP7 has the least memory usage. After removing the PHP 5.2 image, the comparison is more obvious.

It can be seen that PHP7 not only greatly improves the algorithm efficiency, but also greatly optimizes the use of large volumes of memory, although the peak memory of small programs is more than the memory used by previous versions ).

1.3 garbage collection functions

In PHP, memory collection can be controlled. We can explicitly disable or enable garbage collection. One way is to modify the configuration, zend. enable_gc = Off to disable garbage collection. It is On by default. Another method is to enable and disable garbage collection by using the gc _ enable () and gc _ disable () functions.

For example, based on the above example, we can turn off garbage collection to get the following data tables and charts.

The Code is as follows:

 
 
  1. gc_disable(); 
  2. $baseMemory= memory_get_usage(); 
  3. class User 
  4. private $uid; 
  5. function __construct($uid) 
  6.     { 
  7. $this->uid= $uid; 
  8.     } 
  9.  
  10. for($i=0;$i<100000;$i++) 
  11. $obj= new User($i); 
  12. $obj->self = $obj; 
  13. if ( $i% 5000 === 0 ) 
  14.     { 
  15. echo sprintf( '%6d: ', $i), memory_get_usage(), " bytes/n"; 
  16.     } 
  17. echo "  peak: ",memory_get_peak_usage(true), " bytes/n"; 

Run in PHP 5.3, PHP5.4, PHP5.5, PHP5.6, and PHP7 respectively. The following memory usage statistics are obtained.

As shown in the figure below, PHP7 is still the most efficient in memory usage.

As shown in the above example, although in the first example, the peak memory usage of PHP 7 is the most, when the memory usage is large, PHP7 memory optimization is reflected.

It is worth mentioning that garbage collection may reduce the memory but speed down, because garbage collection also consumes CPU and other system resources. The Composer project once shut down garbage collection before computing dependencies, resulting in exponential performance improvement, attracting the attention of the majority of users. For details, see:

Https://github.com/composer/composer/commit/ac676f47f7bbc619678a29deae097b6b0710b799

In common code and performance analysis, in addition to the above three types of functions, stack trace functions and output functions are often used.


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.