PHP array read loop operation

Source: Internet
Author: User
PHP is basically an array language. There are often a lot of array loop operations, mainly in two ways, one is foreach, the other is while, in the end, which is good or bad is always controversial, although I have been aware of this problem for a long time, I have not studied it in detail. I have been ignorant of it until now. To save some CPU time in the future, I will summarize it as follows: if the array "read" operation is performed in the loop, foreach is faster than while: No format viewing is copied to clipboard Printing Code ? Foreach ($ array as $ value) {echo $ value;} while (List ($ key) = each ($ array) {echo $ array [$ key];} foreach ($ array as $ value) {echo $ value;} while (List ($ key) = each ($ array) {echo $ array [$ key];} when the array "write" operation is performed in a loop, while is faster than foreach: No format to view the code copied to the clipboard? Foreach ($ array as $ key =>$ value) {echo $ array [$ key] = $ value. '... ';} while (List ($ key) = each ($ array) {$ array [$ key] = $ array [$ key]. '... ';} foreach ($ array as $ key => $ value) {echo $ array [$ key] = $ value. '... ';} while (List ($ key) = each ($ array) {$ array [$ key] = $ array [$ key]. '... ';} conclusion: Generally, foreach involves value replication, which is certainly slower than while, but in fact, if it is just reading the array in a loop, foreach is very fast, this is because the replication mechanism used by PHP is "reference replication, copy upon write". In this case, It is easy to understand the efficient read operations of each. In addition, since foreach is not suitable for Array write operations, we can draw a conclusion that, in most cases, it is similar to foreach ($ array as $ key => $ value) all code in the form should be replaced with while (List ($ key) = each ($ array )). The speed differences produced by these techniques may not be obvious in small projects, but in large projects like frameworks, a single request may involve hundreds of thousands of array loop operations, the difference is obviously magnified.

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.