日期:2014-05-20  浏览次数:20715 次

对集合使用for each循环和Iterator的效率问题
RT,java中使用这两种方法哪个的效率高。还有,和普通的for循环比起来,哪个效率更高。

------解决方案--------------------
拿 foreach 迭代集合实际也是拿 Iterator 迭代的,语法上更简洁了。
------解决方案--------------------
反编译一下可以看到里面还是通过迭代器来实现的。
文档里也有说明:
public interface Iterable<T>实现这个接口允许对象成为 "foreach" 语句的目标。 
编译之后通过Iterator<T> iterator()方法获得的迭代器来实现循环访问。
------解决方案--------------------
对于ArrayList,Vector 其核心是一个数组, 如果明确知道List的实例是ArrayList,Vector,当然用 for(int i=0; i<lst.size();i++){} 这种方式是最快的. 当然用Iterator 的话,其实是相关无几,Iterator 基本上都是指针操作, Iterator本身的处理会增加一点点的开销,跟踪一下源码就可以知道.

Iterator 好处:通用,对于所有集合,使用Iterator性能都一样, 客户端自身不维护遍历集合的"指针",所有的内部状态(如当前元素位置,是否有下一个元素)都由Iterator来维护,而这个Iterator由集合类通过工厂方法生成,因此,它知道如何遍历整个集合。
客户端从不直接和集合类打交道,它总是控制Iterator,向它发送"向前","向后","取当前元素"的命令,就可以间接遍历整个集合。

for(i=0;...) 方法有一个缺点: 如果List的实例是LinkedList等非"数组"存储方式的时候,遍历集合的开销会差别很大! 就以LinkedList来说,以下是get(i)方法来取元素的主要代码, 我们可以看到,LinkedList 内的get(i)方法,用了循环方式来返回元素,性能肯定会差.
Java code

        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException("Index: "+index+
                                                ", Size: "+size);
        Entry<E> e = header;
        if (index < (size >> 1)) {
            for (int i = 0; i <= index; i++)
                e = e.next;
        } else {
            for (int i = size; i > index; i--)
                e = e.previous;
        }
        return e;

------解决方案--------------------
楼主,我用100000最小数据测试了一下:
Java code

import java.util.*;
public class CMPfor
{
    final long N = 1000000;
    private ArrayList<Integer> aT = new ArrayList<Integer>();

    private LinkedList<Integer> LT = new LinkedList<Integer>();

    public CMPfor(){
        for(int i = 0; i < N; i++){
            aT.add(i);
        }
        for(int i = 0; i < N; i++){
            LT.add(i);
        }
    }

    public void forTest(List<Integer> t){
        for(int i = 0; i < t.size();i++){
            t.get(i);
        }
    }


    public void foreachTest(List<Integer> t){
        for(int j : t){
            
        }
    }

    public void iteratorTest(List<Integer> t){
        Iterator it = t.iterator();
        while(it.hasNext()){
            it.next();
        }
    }


    public void whileTest(List<Integer> t){
        int i = 0;
        while(i < N){
            t.get(i);    
            i++;
        }
    }

    public static void main(String[] args){
    
        CMPfor cf = new CMPfor();

        long a,b;

        System.out.println("ArrayList Test:");
        a=System.currentTimeMillis();

        cf.forTest(cf.aT);

        b=System.currentTimeMillis();

        System.out.println("forTest:    "+(b-a)+"ms");

        a=System.currentTimeMillis();

        cf.foreachTest(cf.aT);

        b=System.currentTimeMillis();

        System.out.println("foreachTest:    "+(b-a)+"ms");

        a=System.currentTimeMillis();

        cf.iteratorTest(cf.aT);

        b=System.currentTimeMillis();

        System.out.println("iteratorTest:    "+(b-a)+"ms");


        a=System.currentTimeMillis();

        cf.whileTest(cf.aT);

        b=System.currentTimeMillis();

        System.out.println("whileTest:    "+(b-a)+"ms");


        System.out.println("LinkedList Test:");
        a=System.currentTimeMillis();

        cf.forTest(cf.LT);

        b=System.currentTimeMillis();

        System.out.println("forTest:    "+(b-a)+"ms");

        a=System.currentTimeMillis();

        cf.foreachTest(cf.LT);

        b=System.currentTimeMillis();

        System.out.println("foreachTest:    "+(b-a)+"ms");

        a=System.currentTimeMillis();

        cf.iteratorTest(cf.LT);

        b=System.currentTimeMillis();

        System.out.println("iteratorTest:    "+(b-a)+"ms");


        a=System.currentTimeMillis();

        cf.whileTest(cf.LT);

        b=System.currentTimeMillis();

        System.out.println("whileTest:    "+(b-a)+"ms");
    }
}