Why count bad than $ count

I just looked through the answers to various questions to find out more. I saw the answer saying that it is bad practice in php to write

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

It says that calling the count function in a loop reduces the speed of the code. The discussion in the comments on this subject was unclear. I want to know why this is not a good practice. What should be an alternative way to do this?

+7
source share
4 answers

Instead, you should do this:

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

The reason for this is that if you put count($array) inside a for loop, then the count function will have to be called for every iteration that slows down the speed.

However, if you put the score in a variable, this is a static number that does not need to be counted every time.

+8
source

For each iteration, PHP checks this part of the loop (condition) to see if it should continue the loop, and each time it checks it, it calculates the length of the array.

An easy way to cache this value ...

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

This is probably not necessary in small cycles, but can be of great importance when repeating several thousand elements and depends on which function call is in state (and how it determines its return value).

That's why you should use foreach() { ... } , if you can, it uses an iterator on a copy of the array to iterate over the loop over the set, and you don’t have to worry about caching the state of the loop.

+4
source

I heard about a database in a doctor’s surgery that made this very mistake using software. It was tested with about 100 entries, everything worked perfectly. For several months, he dealt with millions of records and was completely unusable, requiring minutes to download results. The code was replaced in accordance with the answers above, and it worked perfectly.

To think about it differently, a fairly powerful dedicated server that does nothing else will take about 1 nanosecond to do the count ($ array). If you had 100 cycles, each of which counted 1000 lines, then only 0.0001 seconds.

However, it is 100,000 calculations to load each page. Scale that up to 1,000,000 users (and who does not want to have 1 million users?) ... does a 10-page download, and now you have calculations for 1,000,000,000,000 (1 trillion). This will lead to a large load on the server. This is the 1000 seconds (about 16.5 minutes) that your processor spends executing this code.

Now increase the time the machine takes to process the code, the number of elements in arrays and the number of loops in the code ... you are talking about literally many trillions of processes and many hours that can be avoided by simply storing the result in a variable.

+3
source

This is not a good practice, because, as written, count($array) will be called every time through a loop. Assuming that you will not resize the array in a loop (which in itself would be a terrible idea), this function will always return the same value, and calling again will be redundant.

For short loops, the difference will probably not be noticeable, but it is best to call the function once and use the calculated value in the loop.

+1
source

All Articles