[Wind Terminator] PHPfor/foreach/while performance comparison

Source: Internet
Author: User
[Rumor Terminator] PHPfor/foreach/while has better performance than my female friend and tells me, well, what? I 'd like to hear others say that the best foreach in php should not use for, because the performance will be better. my first reaction was that it was impossible. The second reaction was to think about why others had such a conclusion. I guess some [Rumor Terminator] PHP for/foreach/while performance comparison
My female friend told me, oh, what? I 'd like to hear other people say that the best foreach in php should not use for, because the performance will be better.

My first reaction was that it was impossible. The second reaction was to think about why others had such a conclusion.

I guess some children's paper testing for probably uses the following form:
For ($ I = 0; $ I
 
  

Child paper, each cycle needs to count the number of items in $ array, which consumes resources, unless PHP of a certain version will optimize this method in the future:
$arraySize = count($array);for($i=0; $i<$arraySize; $i++){...}


PHP's internal optimization of foreach is also like this.

Then I went online and found out how the experiment of the child paper that holds the idea of "foreach is more efficient than for". I found one of the results:

Http://www.phpq.net/research/foreach-while-for.html

As I thought, TA used the following form to test:
for($i = 0; $i < count($arr); $i++){$str .= $arr[$i];}


I changed the experiment case of TA:
$ Size = count ($ arr );
For ($ I = 0; $ I <$ size; $ I ++ ){
$ Str. = $ arr [$ I];

Efficiency comes up.

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.