PHP reference is a bad habit _ PHP Tutorial

Source: Internet
Author: User
PHP reference is a bad habit. Copy the code as follows: functionbinsearch ($ highcount ($ arr); while ($ low $ high) {$ midfloor ($ low + ($ high-$ low) 2 ); $ item $ arr [$ mid] [$ key]; if ($ item $ value ){ The code is as follows:


Function binsearch (& $ arr, $ key, $ value)
{
$ Low = 0;
$ High = count ($ arr );
While ($ low <= $ high ){
$ Mid = floor ($ low + ($ high-$ low)/2 );
$ Item = $ arr [$ mid] [$ key];
If ($ item = $ value ){
Return $ mid;
} Else if ($ value> $ item ){
$ Low = $ mid + 1;
} Else {
$ High = $ mid-1;
}
}
Return false;
}


Here, $ mid is calculated by first subtraction and then addition to prevent integer overflow. It is not intentionally complicated to write.
I use the following code for testing:

The code is as follows:


$ Data = array ();
For ($ I = 0; I I <1000000; $ I ++)
{
$ Data [] = array ("sq" => $ I * 2 );
}
Var_dump (binsearch ($ data, "sq", 10000 ));


It is found that binsearch always takes about seconds. Theoretically, 1 million of the data is up to 20 cycles. How can it be so slow.
Later I monitored the memory, and the data array occupied 230 MB of memory. Binsearch consumes 60 kB of memory. However, in theory, binsearch
It should not occupy so much memory. Because, I think, I have used references, and I have not modified the data structure at all.
I was puzzled, too. later, I removed the reference parameter, so that binsearch only needed 0.0002 s. It seems that the reference consumes a lot of cpu resources.
PHP adopts the principle of copy on write internally. In fact, this reference is redundant.
But why is the reference speed slower? Today we will focus on this issue. After understanding the truth, you must know how to use references.
If $ a = & $ data is directly called before binsearch, the reference speed will be very fast. It seems that it is definitely not a problem of reference itself.
This actually involves how the zend Engine manages PHP variables.
Let's take a look at the following questions:

The code is as follows:


Function demo (& $ a, & $ B) {$ a = & $ B ;}
$ A = 1;
$ B = 2;
Demo ($ a, $ B );
$ B = 3;
Print $;
?>


$ A What is the output? Yes, it's 2. but I thought it was 3 at first.
So how can we explain the problem above?
In fact, the parameter reference of a function is implemented in this way.

The code is as follows:


$ Tmp = $;
$ A1 = & $ tmp;
$ A = $ tmp;
Unset ($ a1, $ tmp );


Here, the reference is actually a temporary variable. At this time, $ tmp includes the reference attribute, while $ a does not.
According to the zend Engine memory management method, a zval cannot be used internally, and the zval must be forcibly separated.
With this understanding method, the above problem is solved. The function does not change the reference feature of the function. This is also PHP
The reason for disapproving calltime_by_ref is that the above inefficient copy method is selected.
The analysis below also proves that a copy was indeed performed when the parameter was passed.
In the binsearch function.
$ Data [0] = 1;
In this way, a copy of the zval where $ data is located will occur. The memory usage is 60 KB. It is exactly the same as the function call plus reference.
A lot of people may wonder why it is not over MB. this is actually the best of PHP. the array Key corresponds to a zval pointer. (An internal hash table)
Therefore, you only need to copy these pointers once, and data does not need to be copied. However, 1 million of PHP hash tables actually occupy 50 MB of memory. Why is it 60 kB.
Run

The code is as follows:


$ T = $ data;
$ T [0] = 1;
Unset ($ t );


Sure enough, 60 K more memory. It is estimated that it is related to the memory management mechanism of PHP.
Now everything is clear! Today, after several hours of thinking, I figured out this problem and did not dare to share it exclusively.
The reference in a function is not convenient for you to pass parameters, but for you to implement it. a function can have multiple return values. Therefore, it is best not to draw a picture.
In fact, referencing it will reduce performance.

The http://www.bkjia.com/PHPjc/321529.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321529.htmlTechArticle code is as follows: function binsearch ($ high = count ($ arr); while ($ low = $ high) {$ mid = floor ($ low + ($ high-$ low) /2); $ item = $ arr [$ mid] [$ key]; if ($ item = $ value ){...

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.