Micro-analysis of PHP performance

Source: Internet
Author: User
Tags add end functions garbage collection memory usage return sprintf
First, 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 a piece of code internal performance analysis, to the second precision is not enough. So you 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 should be noted that, by default, the precision returned is only 4 decimal digits.   In order to achieve higher accuracy, we need to configure precision. The following are the results of the use 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 &NB Sp   0.0002031326293945312 In addition to Microtime Intrastat, can also use Getrusage to obtain user state of the matter long. In the actual operation, also commonly used time command to calculate the length of the entire program, by running or modifying the code several times after the operation, to get different lengths to achieve the difference in efficiency. The specific use is: Time phptime.php, then after the program is completed, whether or not the normal end of exit, there will be related statistics.       bash-3.2# time phptime.php       1441360373.150756     1441360373.150959 &NB Sp   0.0002031326293945312       Real    0m0.186s     user    0m0.072s &nbs P   SYS     0m0.077s because of the performance problems discussed in this article, we often analyze the gaps and trends after millions of calls, in order to avoid some time statistics code in the code, later we use the Times command mostly.   1.2, Memory use correlation function analysis memory uses two functions: Memory_ get_ usage, memory_ get_ peak_usage, the former can obtain the program at the call point in time, that is, the current use of memory, the latter can be obtained to the currentThe memory used during peak periods. 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 can be seen, even if the program only output a word, plus variable storage, also consumes 168 bytes of memory. For the same program, the use of different PHP versions of memory is not the same, and even significantly different.
   $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
    ...
    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 of some extensive. PHP 5.4-5.6 is similar, has the optimization:
 [root@localhostphpperf]# php56 memory.php


    0:224944 bytes 10000:224920
    ...
    80000:224920 bytes
    90000:224920 bytes
    peak:262144 bytes
and PHP 7 in a small amount of use, peak memory usage, increased a lot.
  [root@localhostphpperf]# php7 memory.php


    0:353912 bytes 10000:353912
    ...
    80000:353912 bytes
    90000:353912 bytes
    peak:2097152 bytes
From the above also see that the use of PHP has a good garbage collection mechanism, 100,000 initialization, and not with the increase in object initialization and increased memory usage.   The PHP7 peak memory is used up to nearly 2M. Here is another example, on the basis of 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";

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.