我是靠谱客的博主 阔达微笑,最近开发中收集的这篇文章主要介绍java中普通for,增强for和forEach效率对比,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.10000复制对比

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(new String());
        }
        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));
        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));
    }
}

输出结果:

普通for时间:2
增强for时间:2
forEach时间:39

2.100000复制对比

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            list.add(new String());
        }
        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));
        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));
    }
}

输出结果:

普通for时间:3
增强for时间:4
forEach时间:38

3.1000000次复制对比

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            list.add(new String());
        }
        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));
        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));
    }
}

输出结果

普通for时间:12
增强for时间:35
forEach时间:45

4.10000000次复制对比

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            list.add(new String());
        }
        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));
        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));
    }
}

输出结果:

普通for时间:169
增强for时间:89
forEach时间:224

5.修改前后位置测试,10000000

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            list.add(new String());
        }

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));

        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));


        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));


    }
}

输出结果:

forEach时间:1717
增强for时间:99
普通for时间:161

6,修改前后位置1000000次复制

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            list.add(new String());
        }

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));

        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));


        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));


    }
}

输出结果:

forEach时间:70
增强for时间:35
普通for时间:11

7.修改前后测试100000次复制

public class DataTestDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            list.add(new String());
        }

        long startTime2 = System.currentTimeMillis();
        //forEach
        list.forEach(s->list3.add(s));
        long endTime2 = System.currentTimeMillis();
        System.out.println("forEach时间:"+(endTime2 - startTime2));

        //增强for
        long startTime1 = System.currentTimeMillis();
        for (String s : list) {
            list2.add(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("增强for时间:"+(endTime1 - startTime1));


        long startTime = System.currentTimeMillis();
        //普通for
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通for时间:"+(endTime - startTime));


    }
}

输出结果:

forEach时间:42
增强for时间:3
普通for时间:2

说明:测试可能有不严谨的地方,时间,机器,执行顺序,cpu运行后效率降低也有可能,仅供参考

最后

以上就是阔达微笑为你收集整理的java中普通for,增强for和forEach效率对比的全部内容,希望文章能够帮你解决java中普通for,增强for和forEach效率对比所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部