为什么增强的 for 循环比普通的 for 循环更有效 [英] why is the enhanced for loop more efficient than the normal for loop

查看:44
本文介绍了为什么增强的 for 循环比普通的 for 循环更有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到 增强的 for 循环 比普通的 for 循环 更有效:

I read that the enhanced for loop is more efficient than the normal for loop here:

http://developer.android.com/guide/practices/performance.html#foreach

当我搜索它们之间的效率差异时,我发现的是:在正常的for循环的情况下,我们需要一个额外的步骤来找出数组的长度或大小等,

When I searched for the difference between their efficiency, all I found is: In case of normal for loop we need an extra step to find out the length of the array or size etc.,

for(Integer i : list){
   ....
}


int n = list.size();
for(int i=0; i < n; ++i){
  ....
}

但这是否是唯一的原因,增强的 for 循环比普通的 for 循环更好?在这种情况下,最好使用普通的 for 循环,因为理解增强的 for 循环 有点复杂.

But is this the only reason, the enhanced for loop is better than the normal for loop? In that case better use the normal for loop because of the slight complexity in understanding the enhanced for loop.

检查一个有趣的问题:http://www.coderanch.com/t/258147/java-programmer-SCJP/certification/Enhanced-Loop-Vs-Loop

谁能解释一下这两种for循环的内部实现,或者解释使用增强的for循环的其他原因?

Can any one please explain the internal implementation of these two types of for loops, or explain other reasons to use the enhanced for loop?

推荐答案

说增强的 for 循环更有效有点过于简单化了.它可以,但在许多情况下,它几乎与老式循环完全相同.

It's a bit of an oversimplification to say that the enhanced for loop is more efficient. It can be, but in many cases it's almost exactly the same as an old-school loop.

首先要注意的是,对于集合,增强的 for 循环使用 Iterator,因此如果您使用 Iterator 手动迭代集合,那么您应该很漂亮与增强的 for 循环的性能大致相同.

The first thing to note is that for collections the enhanced for loop uses an Iterator, so if you manually iterate over a collection using an Iterator then you should have pretty much the same performance than the enhanced for loop.

增强的 for 循环比简单实现传统循环更快的地方是这样的:

One place where the enhanced for loop is faster than a naively implemented traditional loop is something like this:

LinkedList<Object> list = ...;

// Loop 1:
int size = list.size();
for (int i = 0; i<size; i++) {
   Object o = list.get(i);
   /// do stuff
}

// Loop 2:
for (Object o : list) {
  // do stuff
}

// Loop 3:
Iterator<Object> it = list.iterator();
while (it.hasNext()) {
  Object o = it.next();
  // do stuff
}

在这种情况下,循环 1 将比循环 2 和循环 3 慢,因为它必须(部分)在每次迭代中遍历列表以找到位置 i 处的元素.然而,由于使用了 Iterator,循环 2 和循环 3 只会在列表中进一步步进一个元素.循环 2 和循环 3 也将具有几乎相同的性能,因为循环 3 几乎与您在循环 2 中编写代码时编译器将产生的结果完全相同.

In this case Loop 1 will be slower than both Loop 2 and Loop 3, because it will have to (partially) traverse the list in each iteration to find the element at position i. Loop 2 and 3, however will only ever step one element further in the list, due to the use of an Iterator. Loop 2 and 3 will also have pretty much the same performance since Loop 3 is pretty much exactly what the compiler will produce when you write the code in Loop 2.

这篇关于为什么增强的 for 循环比普通的 for 循环更有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆