Performance Comparison between PHP loop functions foreach and while

Source: Internet
Author: User
Tags foreach

Foreach performs operations on array replicas (by copying arrays), while operations are performed by moving internal indicators of the array. Generally, it is logically considered that, while should be faster than foreach (because foreach first copies the array when starting execution, while directly moves the internal indicator .), But the result is just the opposite.

The code is as follows: Copy code

/**
* Performance Comparison Between while and foreach functions
 *
*/

// Foreach function
Foreach ($ array as $ key => $ value ){
Echo $ array [$ key] = $ value .'...';
}


// While function
While (list ($ key) = each ($ array )){
$ Array [$ key] = $ array [$ key]. '...';
}


If the array "read" operation is performed in a loop, foreach is faster than while:
If the array "write" operation is performed in a loop, while is faster than foreach

To prove some of the best bits, let's test them now.

First, let's test the time used to traverse a one-dimensional array with 50000 sub-standards:

 
Test platform:
CPU: P-M 725
Memory: 512 MB
Hard disk: 40 GB 5400 rpm
OS: Windows XP SP2
WEB: apache 2.0.54 php5.0.4
Test code:

The code is as follows: Copy code

<? Php
/*
* @ Author: Lilov
* @ Homepage: www.111cn.net
* @ E-mail: zhongjiechao@gmail.com
  *
*/
$ Arr = array ();
For ($ I = 0; I 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) <br> ';
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) <br> ';
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) <br> ';
######################################
?>

Test results:
Calculate the average value of the three test results:
Corresponding to for, while, and foreach respectively
0.1311650
0.1666853
0.1237440

After repeated tests, the results show that for traversing the same array, foreach is the fastest, while is the slowest. Foreach is about 20% ~ faster than while ~ About 30%. Then add the array subscript to 500000 and 5000000. However, in principle, foreach operates on the array copy (by copying the array), while moves the internal indicator of the array. In general logic, while should be faster than foreach (because foreach first copies the array when starting execution, while directly moves the internal indicator .), But the result is just the opposite. The reason should be that foreach is implemented inside PHP, while is a general loop structure.
Therefore, in general applications, I prefer the foreach format, which is simple and highly efficient. In PHP5, foreach can also traverse class attributes.

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.