PHP Array Traversal performance comparison _php tutorial

Source: Internet
Author: User
The For loop is limited to numeric indexes, and the for and foreach loops do not require the reset () operation to be performed for the next traversal, and each method requires

!--? php tutorial
//a
$arr =array (' a ' = ' abc ', ' B ' =>123, ' C ' =>true);
b
//$arr =range (' A ', ' d ');

//1
for ($i =0; $i echo $arr [$i]. ', ';
Echo '
';

//2
foreach ($arr as $key)
echo "$key,";
Echo '
';

//3
foreach ($arr as $key = + $val)
echo "$key-$val,";
Echo '
';

//4
Reset ($arr);
while ($item =each ($arr)) {
echo $item [' key ']. -'. $item [' value ']. ', ';
}
Echo '
';

//5
Reset ($arr);
while (list ($key, $val) =each ($arr)) {
echo ' $key-$val, ";
}
Echo '
';
? > Use Statement a $arr =array (' a ' = ' = ' abc ', ' B ' =>123, ' C ' =>true); Initialize the $arr to get a numeric index array, output as follows:

,,,
ABC, 123, 1,
A-abc, b-123, C-1,
A-abc, b-123, C-1,
A-abc, b-123, c-1, make Use statement b $arr =range (' A ', ' d '); Initialize the $arr to get an associative array with the output as follows:

A, B, C, D,
A, B, C, D,
0-a, 1-b, 2-c,-D,
0-a, 1-b, 2-c, D-,
0-a, 1-b, 2-c, -I,

After repeated tests, the result shows that for traversing the same array, the foreach speed is the fastest and the slowest is the while. The foreach is about 20% ~ 30% faster than while. The array subscript is then incremented to 500000, and the 5000000 test results are the same. In principle, however, 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 for this is that foreach is an internal implementation of PHP, while the while is a common loop structure


http://www.bkjia.com/PHPjc/444745.html www.bkjia.com true http://www.bkjia.com/PHPjc/444745.html techarticle The for loop is only limited to numeric indexes, and the for and foreach traversal does not require the reset () operation to be performed for the next iteration, and each method needs to be? PHP tutorial//a $arr =array (' a ' = ' a ...

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