PHP traversal array of three methods and comparative analysis of efficiency, the comparative analysis of the _php tutorial

Source: Internet
Author: User

PHP traversal array of three methods and efficiency comparison analysis, the comparison analysis


In this paper, we analyze three methods and efficiency comparison of PHP traversal array. Share to everyone for your reference. The specific analysis is as follows:

A friend of mine asked me a question today. PHP iterates through the array, telling her a few. By the way, write a summary of the article, if the summary is not complete also ask friends to point out

First, foreach ()

foreach () is the simplest and most efficient way to iterate through the data in an array.

<?php     $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');    foreach ($urls as $url) {       
"; } ? >

Show Results:

This site URL is the AAA this site URL is the BBB this site URL is the CCC this site URL is ddd

Second, while () and list (), each () is used together.

<?php     $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');     while (list ($key, $val) = each ($urls)) {       echo ' This Site URL is $val.
";

Show Results:

This site URL is aaathis site URL was bbbthis site URL is cccthis site URL is ddd

Third, for () use for traversal array

<?php     $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');     for ($i = 0; $i < count ($urls), $i + +) {       $str = $urls [$i];       echo "This Site URL is $str.
"; } ? >

Show Results:

Sometimes someone is asking these kinds of ways to iterate over an array which is quicker, and the following is a simple test to understand

Here's a test of the speed of three traversal arrays.

In general, there are three ways to traverse an array, for, while, foreach. One of the simplest and most convenient is foreach. Let's test the time spent together traversing a one-dimensional array with 50,000 subscripts.

<?php $arr = Array ();   for ($i = 0; $i < 50000; $i + +) {$arr []= $i *rand (1000,9999);   } function GetRuntime () {list ($usec, $sec) =explode ("", Microtime ());   return (float) $usec + (float) $sec);   } ###################################### $time _start= getruntime ();   for ($i = 0; $i < count ($arr), $i + +) {$str = $arr [$i];   } $time _end= getruntime ();   $time _used= $time _end-$time _start; Echo ' used time of For: '. Round ($time _used, 7). ' (s)

'; Unset ($str, $time _start, $time _end, $time _used); ###################################### $time _start= getruntime (); while (list ($key, $val) = each ($arr)) {$str = $val; } $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' used time of while: '. Round ($time _used, 7). ' (s)

'; Unset ($str, $key, $val, $time _start, $time _end, $time _used); ###################################### $time _start= getruntime (); foreach ($arr as$key=> $val) {$str = $val; } $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' Used time of foreach: '. Round ($time _used, 7). ' (s)

';?>

Test results:

Used time of for:0.0228429 (s) used time of while:0.0544658 (s) used time of foreach:0.0085628 (s)

After repeated tests, the result shows that for traversing the same array, the foreach speed is the fastest and the slowest is the while. In principle, foreach operates on an array copy (by copying the arrays), while the while is manipulated by moving the internal indicators of the array, which is generally considered to be faster than foreach (since foreach first copies the array in the beginning of execution). While the while moves the internal indicator directly. ), but the result is just the opposite. The reason should be that foreach is an internal implementation of PHP, while the while is a common loop structure. Therefore, foreach is simple and efficient in common applications. Under PHP5, foreach can also traverse the properties of a class.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/957118.html www.bkjia.com true http://www.bkjia.com/PHPjc/957118.html techarticle PHP traversal of the array of three methods and comparative analysis of efficiency, a comparative analysis of the example of PHP three methods and efficiency comparison of the traversal array. Share to everyone for your reference. Specific points ...

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