成都创新互联网站制作重庆分公司

ArrayList与LinkedList的性能对比

ArrayList与LinkedList都是继承至Collection对象,内部实现导致性能方面存在一定的区别

10年积累的成都做网站、网站建设、外贸营销网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有莲池免费网站建设让你可以放心的选择与我们合作。

  1. 插入性能对比

    private final static int COUNT = 10000000;
    
    private static long insertObject(List list) {
        Object obj = new Object();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            list.add(obj);
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }
    
    public static void main(String[] args) {
        int initCount = (int) (COUNT * 1.5);
        System.out.println("ArrayList 增加" + COUNT + "条数据,耗时:" + insertObject(new ArrayList(initCount)));
        System.out.println("LinkedList 增加" + COUNT + "条数据,耗时:" + insertObject(new LinkedList()));
    }

    上述代码得出下列结果:
    ArrayList与LinkedList的性能对比

  2. 查询性能对比
  3.     private final static int COUNT = 1000000;
    
        private static Map insertObject(List list) {
            User user = new User();
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < COUNT; i++) {
                user.setId(i);
                user.setName("i:" + i);
                user.setAge(i);
                list.add(user);
            }
            long endTime = System.currentTimeMillis();
            Map result = new HashMap();
            result.put("time", endTime - startTime);
            result.put("list", list);
            return result;
        }
    
        private static long readObject(List list) {
            long startTime = System.currentTimeMillis();
            for (User user : list) {
    
            }
            long endTime = System.currentTimeMillis();
            return endTime - startTime;
        }
    
        public static void main(String[] args) {
            // 插入
            int initCount = (int) (COUNT * 1.5);
            Map arrayListResult = insertObject(new ArrayList());
            Map linkedListResult = insertObject(new LinkedList());
            System.out.println("ArrayList 增加" + COUNT + "条数据,耗时:" + arrayListResult.get("time"));
            System.out.println("LinkedList 增加" + COUNT + "条数据,耗时:" + linkedListResult.get("time"));
    
            // 读取
            System.out.println("ArrayList 查询" + COUNT + "条数据,耗时:" + readObject((List) arrayListResult.get("list")));
            System.out.println("LinkedList 查询" + COUNT + "条数据,耗时:" + readObject((List) linkedListResult.get("list")));
        }

    结果如下所示:
    ArrayList与LinkedList的性能对比

    3.LinkedList的优势

    
        static final int N = 500000;
    
        static long timeList(List list) {
            long start = System.currentTimeMillis();
            Object o = new Object();
            for (int i = 0; i < N; i++) {
                list.add(0, o);
            }
            long endTime = System.currentTimeMillis();
            return endTime - start;
        }
    
        static long readList(List list) {
            long start = System.currentTimeMillis();
            for (int i = 0, j = list.size(); i < j; i++) {
    
            }
            return System.currentTimeMillis() - start;
        }
    
        static List addList(List list) {
            Object o = new Object();
            for (int i = 0; i < N; i++) {
                list.add(0, o);
            }
            return list;
        }
    
        public static void main(String[] args) {
            System.out.println("ArrayList添加" + N + "条耗时:" + timeList(new ArrayList()));
            System.out.println("LinkedList添加" + N + "条耗时:" + timeList(new LinkedList()));
    
            List list1 = addList(new ArrayList<>());
            List list2 = addList(new LinkedList<>());
            System.out.println("ArrayList查找" + N + "条耗时:" + readList(list1));
            System.out.println("LinkedList查找" + N + "条耗时:" + readList(list2));
        }
    

    结果:
    ArrayList与LinkedList的性能对比

    LinkedList的优势在于头部插入,而不是随机插入,在使用过程中需要根据自己的需求进行选择!!!


    标题名称:ArrayList与LinkedList的性能对比
    网页链接:http://cxhlcq.com/article/jddioc.html

    在线咨询

    微信咨询

    电话咨询

    028-86922220(工作日)

    18980820575(7×24)

    提交需求

    返回顶部