Performance Comparison of foreach, while, and

Source: Internet
Author: User
Generally, there are three methods to traverse an array: for, while, and foreach. Among them, the easiest and most convenient is foreach. What are their differences in operation and performance? it is usually better to use that method. Next let's test the time it takes to traverse a one-dimensional array with 50000 sub-standards: test platform: CPU: P-M725 "> <LINKhref =" http :/

Generally, there are three methods to traverse an array: for, while, and foreach. Among them, the easiest and most convenient is foreach. What are their differences in operation and performance? it is usually better to use that method.
Next, let's test the time it takes to traverse a one-dimensional array with 50000 subtopics:

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:
/*
* @ Author: Lilov
* @ Homepage: www.codesky.com
* @ 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)

';
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: 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, and 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.