Stream性能比较测试

测试环境:jdk1.8.0_60, guava-18.0.jar

toMap

    public static void demo1() {
        int size = 50000;
        List<String> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            list.add(Integer.toString(i));
        }

        System.out.println("size = " + size);
        long start2 = System.currentTimeMillis();
        Map<Integer, String> map3 = Maps.newHashMapWithExpectedSize(size);
//        for (int i = 0; i < size; i++) {
//            String string = list.get(i);
//            map3.put(string.hashCode(), string);
//        }
        for (String string : list) {
            map3.put(string.hashCode(), string);
        }
        long end2 = System.currentTimeMillis();
        System.out.println("iterator --- t = " + (end2 - start2));

        long start = System.currentTimeMillis();
        Map<Integer, String> map = list.parallelStream().collect(
                Collectors.toMap(s -> s.hashCode(), s -> s));
        long end = System.currentTimeMillis();
        System.out.println("stream --- t = " + (end - start));

        long start1 = System.currentTimeMillis();
        Map<Integer, String> map1 = Maps.uniqueIndex(list,//lists为value的列表
                new com.google.common.base.Function<String, Integer>() {
                    @Override
                    public Integer apply(String value) {//返回值是key,参数是value
                        return value.hashCode();
                    }
                });
        long end1 = System.currentTimeMillis();
        System.out.println("guava --- t = " + (end1 - start1));
    }
注意guava生成的Map类型。
测试结果:
size = 5000
iterator --- t = 25
stream --- t = 96
guava --- t = 26

size = 500000
iterator --- t = 148
stream --- t = 1533
guava --- t = 104

filter

    public static void demo2() {
        int size = 5000;
        List<String> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            list.add(Integer.toString(i));
        }

        System.out.println("szie = " + size);
        long start2 = System.currentTimeMillis();
        List<String> list2 = Lists.newArrayListWithCapacity(size);
        for (int i = 0; i < size; i++) {
            String string = list.get(i);
            if (Integer.valueOf(string) % 2 == 0) {
                list2.add(string);
            }
        }

        long end2 = System.currentTimeMillis();
        System.out.println("iterator --- t = " + (end2 - start2));

        long start = System.currentTimeMillis();
        List<String> list1 = list.parallelStream().filter(s -> Integer.valueOf(s) % 2 == 0)
                .collect(Collectors.toList());
        long end = System.currentTimeMillis();
        System.out.println("stream --- t = " + (end - start));
    }
测试结果:
szie = 5000
iterator --- t = 15
stream --- t = 100

szie = 500000
iterator --- t = 104
stream --- t = 133

anyMatch

    public static void demo3() {
        int size = 500000;
        List<String> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            list.add(Integer.toString(i));
        }

        System.out.println("szie = " + size);
        long start2 = System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            String string = list.get(i);
            if (string.endsWith("412347")) {
                break;
            }
        }

        long end2 = System.currentTimeMillis();
        System.out.println("iterator --- t = " + (end2 - start2));

        long start = System.currentTimeMillis();
        list.parallelStream().anyMatch(s -> s.endsWith("1234567"));
        long end = System.currentTimeMillis();
        System.out.println("stream --- t = " + (end - start));
    }
测试结果:
szie = 500000
iterator --- t = 84
stream --- t = 114






你可能感兴趣的:(Stream性能比较测试)