ArrayList与Linked性能分析

来源:互联网 发布:谷歌seo外链博客 编辑:程序博客网 时间:2024/05/17 02:42

插入/删除:

首尾插入时LinkedList效率更高,指定索引位置插入的话就不一定了,因为ArrayList可能会导致对象移位或者扩容复制,LinkedList需要先查找到index前的对象;

查找:

首尾查找效率都很高

指定索引查找的话ArrayList完胜

遍历:

用迭代器遍历的话效率都差不多

public class TestGetMethod {    public static void main(String[] args) {        List<String> al = new ArrayList<String>();        List<String> ll = new LinkedList<String>();                int count = 1000000;        for(int i=0; i<count; i++) {            al.add("");            ll.add("");        }                long ts = System.currentTimeMillis();        for(String s: al) {        }        long te = System.currentTimeMillis();        System.out.println("al iterator spend time:" + (te-ts));                        ts = System.currentTimeMillis();        for(String s: ll) {        }        te = System.currentTimeMillis();        System.out.println("ll iterator spend time:" + (te-ts));                ts = System.currentTimeMillis();        for(int i=0; i<count; i++) {            al.get(i);        }        te = System.currentTimeMillis();        System.out.println("al get spend time:" + (te-ts));                ts = System.currentTimeMillis();        for(int i=0; i<count; i++) {            ll.get(i);        }        te = System.currentTimeMillis();        System.out.println("ll get spend time:" + (te-ts));    }}


0 0
原创粉丝点击