which one is the fastest for iterating through arrays in php? or does another exist which is also faster for iterating through arrays?
The foreach loop is considered to be much better in performance to that of the generic for loop. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.
forEach is almost the same as for or for..of , only slower. There's not much performance difference between the two loops, and you can use whatever better fit's the algorithm. Unlike in AssemblyScript, micro-optimizations of the for loop don't make sense for arrays in JavaScript.
The 'foreach' is slow in comparison to the 'for' loop. The foreach copies the array over which the iteration needs to be performed. For improved performance, the concept of references needs to be used.
The while loop is very common loop among all languages and PHP is not different. In fact, while loop is one of the most popular method to iterate over PHP array. It means that, while the given expression (or condition) is true, execute the code inside the curly brackets, and check the expression again.
In general there is no applicable speed differences between the three functions.
To provide benchmark results to demonstrate the efficiency of varying methods used to iterate over an array from 1 to 10,000
.
Benchmark results of varying PHP versions: https://3v4l.org/a3Jn4
while $i++: 0.00077605247497559 sec
for $i++: 0.00073003768920898 sec
foreach: 0.0004420280456543 sec
while current, next: 0.024288892745972 sec
while reset, next: 0.012929201126099 sec
do while next: 0.011449098587036 sec //added after terminal benchmark
while array_shift: 0.36452603340149 sec
while array_pop: 0.013902902603149 sec
Takes into consideration individual calls for count
with while
and for
$values = range(1, 10000);
$l = count($values);
$i = 0;
while($i<$l){
$i++;
}
$l = count($values);
for($i=0;$i<$l;$i++){
}
foreach($values as $val){
}
The below examples using while
, demonstrate how it would be used less efficiently during iteration.
When functionally iterating over an array and maintaining the current position; while
becomes much less efficient, as next()
and current()
is called during the iteration.
while($val = current($values)){
next($values);
}
If the current positioning of the array is not important, you can call reset()
or current()
prior to iteration.
$value = reset($values);
while ($value) {
$value = next($values);
}
do ... while
is an alternative syntax that can be used, also in conjunction with calling reset()
or current()
prior to iteration and by moving the next()
call to the end of the iteration.
$value = current($values);
do{
}while($value = next($values));
array_shift
can also be called during the iteration, but that negatively impacts performance greatly, due to array_shift
re-indexing the array each time it is called.
while($values){
array_shift($values);
}
Alternatively array_reverse
can be called prior to iteration, in conjunction with calling array_pop
. This will avoid the impact from re-indexing when calling array_shift
.
$values = array_reverse($values);
while($values) {
array_pop($values);
}
In conclusion, the speed of while
, for
, and foreach
should not be the question, but rather what is done within them to maintain positioning of the array.
Terminal Tests run on PHP 5.6.20 x64 NTS CLI:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With