Analysis of strange PHP reference efficiency _ PHP Tutorial

Source: Internet
Author: User
Analysis of strange PHP reference efficiency issues. Function: Copy the code as follows: functionupdate_timelist ($ arr [timequeue]; while (! Empty ($ timequeue [0]) ($ timestamp-$ timequeue [0]) $ threshold) {array_shift ($ timequeue);} function a is as follows:

The code is 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 );
}
}


Do you see any problems with this function? In fact, there is a big problem:

$ Timequeue = & $ arr ['timequeue '];

This line causes the program to read 22 M data and generate a time node linked list to take nearly 40 seconds, while deleting this line changes to using $ arr ['timequeue '] to shorten the time by 30 seconds, it takes about 10 seconds to process 22 M.

The code is as follows:


Function update_timelist (& $ arr, $ timestamp, $ threshold ){
While (! Empty ($ arr ['timequee'] [0]) & ($ timestamp-$ arr ['timequee'] [0])> $ threshold ){
Array_shift ($ arr ['timequeue ']);
}
Array_push ($ arr ['timequeue '], $ timestamp );
If ($ arr ['count'] $ Arr ['count'] = count ($ arr ['timequeue ']);
}


Have you seen the problem? The problem lies in the count function. PHP marks the real content Space pointed to by the variable as a reference type and a non-reference type, as shown in the following code:

The code is as follows:


$ A = 'jb51. net ';
$ B = $;
$ C = $ B;


The actual memory usage is only one copy. because the zend Engine of PHP uses the copy on writing mechanism, a copy of 'jb51 is copied only when $ B and $ c are modified.. net. 'jb51.. net 'content space type is not reference type, if changed to the following code:

The code is as follows:


$ A = 'jb51. net ';
$ B = $;
$ C = & $;


What will happen to this change? Is it still a copy of memory space to store 'jb51. net? No, because $ c is a reference of $ a, and the bucket to which $ a points must be marked as a reference type, you must copy 'jb51 for $ B separately. net, because $ B points to a non-reference type.
As we can understand, $ c is now referenced by $ a. If $ B still executes the space of $ a, modifying $ c will cause $ B to be modified, therefore, if a reference occurs, you must copy it even if no write operation is performed. You can also understand that php only points to the memory space of variables in two types: non-reference and reference. The two types cannot be mixed and cannot be transferred. If you need to change the status of the memory space somewhere, copy it.
The following explains why $ timequeue = & $ arr ['timequeue '] causes slow count. do you still remember the c function call process? Actually, we need to copy the input parameters and pass them in php. However, because of the copy on writing mechanism, count does not actually copy the input parameters when it is not of the reference type, however, $ timequeue = & $ arr ['timequeue '] specifies the memory space of $ timequeue to be of the reference type, while count needs to be of a non-reference type, in this case, count needs to copy $ arr ['timequeue. Directly input $ arr ['timequeue ']. why? Of course, count uses the copy on writing mechanism. what about array_shift and array_push? They are passed in references. you don't have to worry about modifying the type of $ arr ['timequee'], but actually passing in an alias for $ arr ['timequeue.

I have just started learning PHP, and the above analysis is not necessarily correct or comprehensive. You can send an email on my homepage to contact me.

The signature code is as follows: function update_timelist ($ arr ['timequeue ']; while (! Empty ($ timequeue [0]) ($ timestamp-$ timequeue [0]) $ threshold) {array_shift ($ timequeue);}...

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.