Php code optimization: phpforeach and for speed comparison test instance

Source: Internet
Author: User
Tags php foreach
The efficiency of foreach is much higher than that of for. a major reason may be that for has to perform many conditional judgments. So foreach can be used in the future, which can increase the efficiency by one time. Experiment is an important way for me to learn computer science. Computer science is not a simple intelligent game, but a tool to transform the world. Mathematical and experimental methods are the basic methods of computer research, and also the basic methods of learning. mathematics exercises our thinking ability, experiment exercises our operational ability, and solves practical problems. Our daily work should be regarded as an experiment. we should summarize what is useful to us from our daily work. For example, to write php code, an important thing is efficiency, which is not efficient. we need to do experiments. The following is my evaluation of several array loop processing methods in php. the test code is simple: Comparison of instance php foreach and for speed (what is PHP foreach ?)

 & $ V) {$ temp = $ temp + 1 ;}$ t2 = microtime (true); $ t = $ t2-$ t1; echo "using foreach does not cost array operations: {$ t} \ n"; $ t1 = microtime (true); foreach ($ test_arr as $ k => & $ v) {$ v = $ v + 1 ;}$ t2 = microtime (true); $ t = $ t2-$ t1; echo "use foreach to operate the array directly: {$ t} \ n "; $ t1 = microtime (true); foreach ($ test_arr as $ k = >&$ v) {addOne ($ v );} $ t2 = microtime (true); $ t = $ t2-$ t1; echo "use foreach to call functions to operate on arrays: {$ t} \ n"; $ t1 = microtime (True); array_walk ($ test_arr, 'addone'); $ t2 = microtime (true); $ t = $ t2-$ t1; echo "cost for using array_walk: {$ t} \ n "; function addOne (& $ item) {$ item = $ item + 1 ;}?>


Execution result:
Use for, no cost for array operation: 0.15388584136963
No array operation cost for using foreach: 0.076934814453125

Use for and directly perform operations on the array: 0.14769005775452
Use foreach to directly operate on arrays: 0.076115131378174

Use for to call functions for Array Operations: 0.32393312454224
Use foreach to call the function to operate on the array: 0.25716996192932
Cost for array_walk: 0.17966890335083

In this experiment, we can conclude that:

The efficiency of foreach is much higher than that of for. a major reason may be that for has to perform many conditional judgments. So foreach can be used in the future, which can increase the efficiency by one time.

If you want to call a function in a loop, it is best to use array_walk, which is twice more efficient than for and 43% more efficient than foreach.

Another note is that if your program requires high efficiency, do not call a function in a very deep loop. to call a function, use array_walk, it is best to write the code directly in the loop.

The above is the php code optimization: php foreach and for speed comparison test instance details, more please follow the first PHP community other related articles!

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.