Parsing between foreach and normal for loop efficiency

Source: Internet
Author: User
Tags php foreach
As you can see, the normal for loop takes less time than the Foreach loop when looping ArrayList;

when looping linklist, the normal for loop takes much more time than a foreach loop.

When I raise the number of cycles to 1 million times, the loop ArrayList, the normal for loop is a little faster than foreach;

But when the normal for loop linklist in the loop, the program dies directly.

need The data of the looping array structure is when used, it is recommended that normal for loop , because the for loop is accessed by subscript, For the data of the array structure, the use of subscript access is better.

need when looping the data of the linked list structure , , which is a bad practice and may cause the system to crash when the amount of data is large.

related recommendations:

php foreach and for statement usage differences Introduction _php tutorial

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.