What’s the best practice?
This :
for ($i = 0; $i < count($array); $i++) {
//stuff
}
Or, what I usually do :
$count = count($array);
for($i = 0; $i < $count; $i++) {
//stuff
}
Is it the same with the magic of compiler optimization?
1
Profile and see it yourself.
In general, the first sample will execute count()
on every iteration. This may drastically reduce the performance.
But:
-
In some languages and/or some cases, compilers may optimize it. But I wouldn’t rely on it, since most of the time, optimizing such thing is risky.
-
The performance difference may be too minor to bother.
For example, in C#, using a for (var i = 0; i < collection.Count(); i++)
on a lazy collection which hits a database may lead to a disaster. No compilers would optimize this, since they won’t know if you’re just reading a read-only database or if you’re reading it while at the same time modifying it as well. But in all cases, you shouldn’t use for
in a first place in such context.
A rule of thumb:
-
Use a variable outside
for
if you know that the collection will not change when you’re in a loop, -
Count the number of elements in a
for
if the collection size might change when you’re in a loop, -
Ask yourself it other techniques, like simply iterating through a collection until it ends, are not more appropriate.
it depends on whether //stuff
changes $array
(i.e. the length of the array).
The first variant is more explicit since you can directly see that the length of the array is the limit of the loop, it could be slightly slower than the second, but in interpreted languages this should not be too much of a problem.
3
The particular optimization in question goes by a couple of names typically hoisting or loop invariant code motion. It is a fairly common optimization performed by compilers. If you are in a compiled language don’t worry about it, it is probably taken care of. If you are in an interpreted language then weather or not it happens is less certain.
Most languages allow you to initialize multiple variables during the declaration of a for
statement. In all languages the condition component of a for
statement is evaluated for each iteration of the loop. So the following for($i=0; $i < foo(); $i++)
would result in the function foo()
being called in each iteration. As long as foo()
continues to return a value higher than $i
the loop to continue.
This is true in C++, C#, Java and PHP.
Now, in the last example let’s assume the function foo()
returns a constant value, but it’s computationally heavy to calculate it. Therefore, calling foo()
for each iteration would slow the execution of the code.
There are two common practices to solve this problem.
$x = foo();
for($i = 0; $i < $x; $i++) {.....}
And
for($i = 0, $x = foo(); $i < $x; $i++) {....}
I prefer the second as it’s easier to read what $x does, and what it’s used for.
EDIT:
To answer the question, if compiler optimization takes place. The answer is No. All compilers that I’ve seen will continue to evaluate the condition statement of a for loop for each iteration. If that condition contains a function call such as count()
then it will be executed each time.
1