对于vs foreach vs而在php中迭代数组的速度更快

str*_*ade 11 php arrays foreach for-loop while-loop

哪一个是最快的迭代PHP中的数组?或者是否存在另一个迭代数组的速度更快?

Pas*_*TIN 19

即使存在任何差异,这种差异也会很小,根本不重要.

如果您对数据库进行了一次查询,那么与循环迭代结果相比,这将花费很长时间,而forvs foreachvs 的永恒辩论while将不会改变一件事 - 至少如果你有合理数量的数据.

所以,使用:

  • 随你喜欢
  • 适合您的编程标准
  • 什么是最适合您的代码/应用程序

在考虑这种微优化之前,还有很多其他事情可以/应该优化.


如果你真的想要一些数字(即使它只是为了好玩),你可以制定一些基准并在实践中看到结果.


Sha*_*nig 9

对我来说,我根据这个选择我的循环:

foreach
Run Code Online (Sandbox Code Playgroud)

在迭代长度为(或可能)未知的数组时使用.

for
Run Code Online (Sandbox Code Playgroud)

在遍历设置了长度的数组时,或者在需要计数器时使用.

while
Run Code Online (Sandbox Code Playgroud)

在迭代数组时使用,其目的是查找或触发某个标志.

现在确实如此,你可以像FOREACH循环一样使用FOR循环,使用count($ array)......所以最终归结为你的个人偏好/风格.


fyr*_*rye 6

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
Run Code Online (Sandbox Code Playgroud)

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){
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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));
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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: 检测结果