Comparison between for loop and foreach loop efficiency in PHP

Source: Internet
Author: User

Yesterday evening, inadvertently heard people say that PHP for loop efficiency than foreach, as much as possible with the For loop can improve PHP efficiency.

Hearing this argument, I was stunned, for each cycle before the judgment, and foreach just move the pointer inside the array, for the efficiency of the higher than foreach?

-------------------

Today, I wrote a simple script that was tested (the following results are averaged over multiple tests)

$max = 100000; $arr = Range (0, $max); $t 1 = Microtime (true); for ($i = 0; $i < $max; $i + +) {$temp = $arr [$i] + 1;} $t 2 = Microtime (true), $for _t = $t 2-$t 1;echo "for loop:". $for _t. " <br> "; $t 1 = Microtime (true); foreach ($arr as $v) {$temp = $v + 1;} $t 2 = Microtime (true), $foreach _t = $t 2-$t 1;echo "foreach Loop:". $foreach _t. " <br> "; echo" Foreach Loop and for loop with a time difference of: ". ($for _t-$foreach _t). " <br> ";

Results:

For loop: 0.00954389572144
foreach Loop: 0.00662207603455
foreach Loop and for loop with a time difference of: 0.00292181968689

---------

So it looks like foreach is 30% higher than for efficiency.

----------------------

Then look at if the array itself is operating

$max = 100000; $arr = Range (0, $max), $t 1 = Microtime (True), for ($i = 0; $i < $max, $i + +) {$arr [$i] = $arr [$i] + 1;} $t 2 = Microtime (true), $for _t = $t 2-$t 1;echo "for loop:". $for _t. " <br> "; $t 1 = Microtime (true); foreach ($arr as $k = = $v) {$arr [$k] = $v + 1;} $t 2 = Microtime (true), $foreach _t = $t 2-$t 1;echo "foreach Loop:". $foreach _t. " <br> "; echo" Foreach Loop and for loop with a time difference of: ". ($for _t-$foreach _t). " <br> ";

Results:

For loop: 0.0129821300507
foreach Loop: 0.0405921936035
The time difference between a foreach loop and A For loop is:-0.0276100635529

---

Direct manipulation of an array of itself finds that the for is 68% higher than foreach

But we can see that in the example above, foreach supports operations on complex key names, whereas for only an array of consecutive numbers with a key name of 0.

--------------

To be fair, look at the following example to change the for loop to also manipulate complex key names

$max = 100000; $arr = Range (0, $max), $t 1 = Microtime (true), $keys = Array_keys ($arr); for ($i = 0; $i < $max; $i + +) {$arr [$keys [$i]] = $arr [$keys [$i]] + 1;} $t 2 = Microtime (true), $for _t = $t 2-$t 1;echo "for loop:". $for _t. " <br> "; $t 1 = Microtime (true); foreach ($arr as $k = = $v) {$arr [$i] = $v + 1;} $t 2 = Microtime (true), $foreach _t = $t 2-$t 1;echo "foreach Loop:". $foreach _t. " <br> "; echo" Foreach Loop and for loop with a time difference of: ". ($for _t-$foreach _t). " <br> ";

Results:

For loop: 0.0401809215546
foreach Loop: 0.0275750160217
foreach Loop and for loop with a time difference of: 0.0126059055328

-----

foreach is 32% higher than for-

-----------------------------------------------------------------------

Conclusion:

1. In most cases, the efficiency of foreach is higher than for.

2. For an array whose key name is continuous, the for is more efficient than foreach.



This article is from the "Dark Forest" blog, so be sure to keep this source http://mysens.blog.51cto.com/10014673/1789463

Comparison between for loop and foreach loop efficiency in PHP

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.