Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why count is bad than $count

I was just reviewing the answers to different questions to learn more. I saw an answer which says that it is bad practice in php to write

for($i=0;$i<count($array);$i++)

It says that calling the count function in the loop reduces the speed of the code. The discussion in the comments on this question was not clear. I want to know why it is not good practice. What should be the alternative way of doing this?

like image 430
Awais Qarni Avatar asked Apr 15 '11 14:04

Awais Qarni


3 Answers

You should do this instead:

$count = count($array);
for($i=0;$i<$count;$i++)...

The reason for doing this is because if you put the count($array) inside the for loop then the count function would have to be called for every iteration which slows down speed.

However, if you put the count into a variable, it is a static number that won't have to be recalculated every time.

like image 76
Naftali Avatar answered Sep 20 '22 17:09

Naftali


For every iteration, PHP is checking that part of the loop (the condition) to see if it should keep looping, and every time it checks it, it is calculating the length of the array.

An easy way to cache that value is...

for($i=0,$count=count($array);$i<$count;$i++) { ... }

It is probably not necessary in small loops, but could make a huge difference when iterating over thousands of items, and dependent on what function call is in the condition (and how it determines its return value).

This is also why you should use foreach() { ... } if you can, it uses an iterator on a copy of the array to loop over the set and you don't have to worry about caching the condition of the loop.

like image 23
alex Avatar answered Sep 21 '22 17:09

alex


I heard of a database in a doctor's surgery that made exactly this mistake with a piece of software. It was tested with about 100 records, all worked fine. Within a few months, it was dealing with millions of records and was totally unusable, taking minutes to load results. The code was replaced as per the answers above, and it worked perfectly.

To think about it another way, a fairly powerful dedicated server that's not doing much else will take about 1 nanosecond to do count($array). If you had 100 for loops, each counting 1,000 rows then that's only 0.0001 of a second.

However, that's 100,000 calculations for EVERY page load. Scale that up to 1,000,000 users (and who doesn't want to have 1 million users?)... doing a 10 page loads and now you have 1,000,000,000,000 (1 trillion) calculations. That's going to put a lot of load on the server. It's a 1,000 seconds (about 16.5 minutes) that your processor spends running that code.

Now increase the time it takes the machine to process the code, the number of items in the arrays, and the number of for loops in the code... you're talking of literally many trillions of processes and many hours of processing time that can be avoided by just storing the result in a variable first.

like image 21
Dan Blows Avatar answered Sep 20 '22 17:09

Dan Blows