PHP development experience: reference is a bad habit _ PHP Tutorial

Source: Internet
Author: User
PHP development experience: reference is a bad habit. When writing a PHP program, many people like to use a reference when passing parameters. Especially when an array is very large, it is more like adding. Functionbinsearch ($ arr, when writing a PHP program, many users prefer to use a reference when passing parameters. Especially when an array is very large, it is more like adding.

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:

$ 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.

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next page

Many users like to use a reference when passing parameters in the callback program. Especially when an array is very large, it is more like adding. Function binsearch ($ arr ,...

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.