温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ArrayList与LinkedList的性能对比

发布时间:2020-07-28 08:42:11 来源:网络 阅读:366 作者:sieda 栏目:编程语言

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

  1. 插入性能对比

    private final static int COUNT = 10000000;
    
    private static long insertObject(List<Object> 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<Object>(initCount)));
        System.out.println("LinkedList 增加" + COUNT + "条数据,耗时:" + insertObject(new LinkedList<Object>()));
    }

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

  2. 查询性能对比
    private final static int COUNT = 1000000;

    private static Map<String, Object> insertObject(List<Object> 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<String, Object>();
        result.put("time", endTime - startTime);
        result.put("list", list);
        return result;
    }

    private static long readObject(List<User> 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<String, Object> arrayListResult = insertObject(new ArrayList<Object>());
        Map<String, Object> linkedListResult = insertObject(new LinkedList<Object>());
        System.out.println("ArrayList 增加" + COUNT + "条数据,耗时:" + arrayListResult.get("time"));
        System.out.println("LinkedList 增加" + COUNT + "条数据,耗时:" + linkedListResult.get("time"));

        // 读取
        System.out.println("ArrayList 查询" + COUNT + "条数据,耗时:" + readObject((List<User>) arrayListResult.get("list")));
        System.out.println("LinkedList 查询" + COUNT + "条数据,耗时:" + readObject((List<User>) linkedListResult.get("list")));
    }

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

3.LinkedList的优势


    static final int N = 500000;

    static long timeList(List<Object> 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<Object>()));
        System.out.println("LinkedList添加" + N + "条耗时:" + timeList(new LinkedList<Object>()));

        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的优势在于头部插入,而不是随机插入,在使用过程中需要根据自己的需求进行选择!!!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI