Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
3  
Depends a great deal on whether count() always returns the same value, and the compiler doesn't always know that. –  Blrfl May 17 '13 at 22:35

4 Answers 4

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.

share|improve this answer

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.

share|improve this answer
1  
it depends on whether //stuff changes $array: No, not really. There may be something else, like a different thread, which is changing the array while we are currently looping through its elements. –  MainMa May 17 '13 at 21:31
1  
Multithreading is not at all mentioned in the OP's question. Furthermore it seems to be PHP code, so this is not a typical issue there. Point taken for concurrent environments though. –  wirrbel May 17 '13 at 21:41
2  
@MainMa In support of wirrbel's answer: in the presence of multithreading, all bets are off. You can't rely on count() regardless of whether it's called each time or only at the beginning. Making sure the example works with multithreading is a whole separate discussion! –  Andres F. May 17 '13 at 23:09

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.

share|improve this answer

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.

share|improve this answer
    
Thanks. Good point! I like your second solution. It's elegant. –  hlapointe May 18 '13 at 4:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.