How to improve productivity the analysis of the problem of PHP citation efficiency

Source: Internet
Author: User
The functions are as follows:

Copy the Code code as follows:


Function Update_timelist (& $arr, $timestamp, $threshold) {
$timequeue = & $arr [' Timequeue '];
while (!empty ($timequeue [0]) && ($timestamp-$timequeue [0]) > $threshold) {
Array_shift ($timequeue);
}
Array_push ($timequeue, $timestamp);
if ($arr [' count ']
$arr [' count '] = count ($timequeue);
}
}


Can you see what's wrong with this function? In fact, there is a big problem, is the function of:
$timequeue = & $arr [' Timequeue '];
This line causes the program to read the 22M data and generate the time node linked list with nearly 40 seconds, and delete the line changed to direct use $arr[' timequeue '] time shortened by 30 seconds, only 10 seconds or so to finish processing 22M.

Copy the Code code as follows:


Function Update_timelist (& $arr, $timestamp, $threshold) {
while (!empty ($arr [' Timequeue '][0]) && ($timestamp-$arr [' Timequeue '][0]) > $threshold) {
Array_shift ($arr [' timequeue ']);
}
Array_push ($arr [' Timequeue '], $timestamp);
if ($arr [' count ']
$arr [' count '] = count ($arr [' timequeue ']);
}


Do you see what the problem is? The problem is on the Count function, no thought of it. PHP points the variables to the true content space tag in order to reference types and non-reference types, like the following code:

Copy the Code code as follows:


$a = ' jb51.net ';
$b = $a;
$c = $b;


There is only one copy of the memory space, because PHP's Zend engine uses the copy on writing mechanism, only in $b, $c modification will be copied a copy of the ' Jb51.net ', at this time ' jb51.net ' content space type is non-reference type, If you change to the following code:

Copy the Code code as follows:


$a = ' jb51.net ';
$b = $a;
$c = & $a;


What's going to change in this? Is it still a memory space to store ' jb51.net '? No, because $c is a $ A reference, $a point to a storage space that needs to be marked as a reference type, you must copy the ' jb51.net ' separately for $b because $b points to a non-reference type.
We can understand that $c is now a $ A reference, and if $b still executes a $ A space then modifying $c will cause $b to also be modified, so at this point a reference must be duplicated even if there is no write operation. It can also be understood that PHP points to a variable memory space only non-reference and reference two types, two types can not be mixed, can not be transferred. If there is a need to change the state of the memory space, a copy is required.
Here's why it's a lot more $timequeue = & $arr [' Timequeue '] causes count to slow, remember the call procedure of the C function? The actual parameters we pass in need to copy a copy of the incoming, PHP is the same, but because the copy on writing mechanism so that count is not true copy when passing in the non-reference type, but $timequeue = & $arr [' Timequeue '] The $timequeue memory space is specified in order to reference the type, and count requires a non-reference type, which results in a copy of $arr[' Timequeue ' for count. Direct incoming $arr[' timequeue '] Why is there no problem? Count is of course using the copy on writing mechanism, Array_shift and Array_push? They are incoming references ah, do not worry about this is not modified $arr[' timequeue ' type but actually passed in $arr[' Timequeue '] an alias.
For PHP I was just beginning to learn, the above analysis is not necessarily correct, not necessarily comprehensive. You can send me a message on my homepage to communicate with me.

The above describes how to improve the efficiency of a strange PHP citation analysis, including how to improve the efficiency of the content, I hope to be interested in PHP tutorial friends helpful.

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