为什么增强循环的效率比正常的循环 [英] why is enhanced for loop efficient than normal for loop
问题描述
我读过增强的for循环比正常循环更有效:
http://developer.android.com/guide/practices/performance.html#foreach
当我搜索他们的效率之间的差异,所有我发现是...在正常的情况下,我们需要一个额外的步骤来找出数组的长度或大小等。,
for(Integer i:list){
$ p $但是这是增强循环的唯一原因是比正常循环更好...在这种情况下更好地使用正常的循环,因为在理解增强的循环。
....
}
int n =列表。尺寸();
for(int i = 0; i< n; ++ i){
....
}
检查这个有趣的问题:http://www.coderanch.com/t/258147/java-programmer-SCJP/certification/Enhanced-Loop-Vs-Loop
任何人都可以解释这两种for循环的内部实现....或者说明使用增强for循环的其他原因
解决方案增强的for循环效率更高,这太简单了。它可以是,但在很多情况下,它几乎和旧式的循环完全一样。
首先要注意的是,对于集合增强for循环使用
Iterator
,所以如果您使用Iterator
手动迭代集合,那么您应该有几乎和增强的for循环一样。
增强for循环比传统循环更快的一个地方是像这样:
LinkedList< Object> list = ...;
//循环1:
int size = list.size();
for(int i = 0; i< size; i ++){
Object o = list.get(i);
///做东西
}
//循环2:
for(Object o:list){
// do stuff
}
//循环3:
Iterator< Object> it = list.iterator(); (it.hasNext()){
Object o = it.next();
// do stuff
}
在这种情况下,循环1将会变慢比循环2和循环3要多,因为它必须(部分地)在每次迭代中遍历列表以找到位置
i
处的元素。循环2和3,但是由于使用了Iterator
,所以只会在列表中进一步添加一个元素。循环2和循环3的性能也几乎相同,因为循环3与编译器在循环2中编写代码时的效果相当。I read that enhanced for loop is efficient than normal for loop here:
http://developer.android.com/guide/practices/performance.html#foreach
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){ .... }
but is this the only reason enhanced for loop is better than normal for loop...in that case better use the normal for loop because of the slight complexity in understanding the enhanced for loop.
check this for an interesting issue: http://www.coderanch.com/t/258147/java-programmer-SCJP/certification/Enhanced-Loop-Vs-Loop
Can any one please explain the internal implementation of these two types of for loops....or explain other reasons to use enhanced for loop
解决方案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.
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 anIterator
then you should have pretty much the same performance than the enhanced for loop.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 }
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 anIterator
. 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.这篇关于为什么增强循环的效率比正常的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!