PHP foreach, while performance comparison _php skills

Source: Internet
Author: User
Tags php foreach
A foreach is an array copy that operates (by copying arrays), whereas while while it operates by moving an array's internal metrics, the general logic is that while should be faster than foreach (because foreach first copies the array when it starts executing), While the while directly moving internal indicators. ), but the result is just the opposite.
In the loop is an array "read" operation, the foreach is faster than the while:
Copy Code code as follows:

foreach ($array as $value) {
Echo $value;
}
while (the list ($key) = each ($array)) {
echo $array [$key];
}

In the loop is the array write operation, while the while is faster than foreach:
Copy Code code as follows:

foreach ($array as $key => $value) {
echo $array [$key] = $value. '...';
}
while (the list ($key) = each ($array)) {
$array [$key] = $array [$key]. '...';
}

Summary: It is generally assumed that foreach involves value copying, which is certainly slower than while, but in fact, if the array is only read in the loop, then foreach is very
Fast, this is because the replication mechanism used in PHP is "reference count, write-time copy", that is, even if you copy a variable in PHP, the original form basically says actually
is still a form of reference, and only when the contents of a variable change, does the true copy occur, and this is done for the purpose of saving memory consumption, but also for improving
The efficiency of replication. In this way, it is easy to understand the efficient read operation of foreach. Also, since foreach is not suitable for handling array writes, we can draw a knot
In most cases, the code that makes an array write in the form of a foreach ($array as $key => $value) should be replaced with a while (the list ($key) =
each ($array)). The speed difference generated by these techniques may not be obvious in small projects, but in large projects such as frameworks, a request can involve a few
Thousands of tens of thousands of times the array loop operation, the difference will be significantly 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.