1st off I'm new to PHP. I have been using for loop,while loop,foreach loop in scripts. I wonder
- which one is better for performance?
- what's the criteria to select a loop?
- which should be used when we loop inside another loop?
the code which I'm stuck with wondering which loop to be used.
for($i=0;$i<count($all);$i++)
{
//do some tasks here
for($j=0;$j<count($rows);$j++)
{
//do some other tasks here
}
}
It's pretty obvious that I can write the above code using while. Hope someone will help me out to figure out which loop should be better to be used.
Performance: Easy enough to test. If you're doing something like machine learning or big data you should really look at something that's compiled or assembled and not interpreted though; if the cycles really matter. Here are some benchmarks between the various programming languages. It looks like
do-while
loop is the winner on my systems usingPHP
with these examples.Criteria: while, for, and foreach are the major control structures most people use in PHP. do-while is faster than
while
in my tests, but largely underused in most PHP coding examples on the web.for
is count controlled, so it iterates a specific number of times; though it is slower in my own results than using awhile
for the same thing.while
is good when something might start out asfalse
, so it can prevent something from ever running and wasting resources.do-while
at least once, and then until the condition returnsfalse
. It's a little faster than awhile
loop in my results, but it's going to run at least once.foreach
is good for iterating through anarray
orobject
. Even though you can loop through a string with afor
statement using array syntax you can't use foreach to do it though in PHP.Control Structure Nesting: It really depends on what you're doing to determine while control structure to use when nesting. In some cases like Object Oriented Programming you'll actually want to call functions that contain your control structures (individually) rather than using massive programs in procedural style that contain many nested controls. This can make it easier to read, debug, and instantiate.
for
or awhile
loop, the number of iterations determine execution time.for
loop. If you want to run and stop on a condition, use awhile
loopCheck http://www.phpbench.com/ for a good reference on some PHP benchmarks.
The for loop is usually pretty fast. Don't include your count($rows) or count($all) in the for itself, do it outside like so:
Placing the count($all) in the for loop makes it calculate this statement for each loop. Calculating the value first, and then using the calculation in the loop makes it only run once.
Who cares? It won't be significant. Ever. If these sorts of tiny optimizations mattered, you wouldn't be using PHP.
Pick the one that's easiest to read and least likely to cause mistakes in the future. When you're looping through integers,
for
loops are great. When you're looping through a collection like an array,foreach
is great, when you just need to loop until you're "done",while
is great.This may depend on stylistic rules too (for example, in Python you almost always want to use a foreach loop because that's "the way it's done in Python"). I'm not sure what the standard is in PHP though.
Whichever loop type makes the most sense (see the answer above).
In your code, the
for
loop seems pretty natural to me, since you have a defined start and stop index.It doesn't matter.
If you just need to walk through all the elements of an object or array, use
foreach
. Cases where you needfor
includeforeach
is much more convenient because it doesn't require you to set up the counting, and can work its way through any kind of member - be it object properties or associative array elements (which afor
won't catch). It's usually best for readability.Both are fine; in your demo case,
foreach
is the simplest way to go.To the best of my knowledge, there is little to no performance difference between while loop and for loop i don't know about the for-each loop