2

我已经下载了 release 并在和java8-ea之间进行了快速比较。Array.sortArrays.parallelSort

结果是这样的: 在此处输入图像描述

我可以理解 praralleSort 至少应该像 Plain old 一样执行sort,如果不是更快的话..但这不是发生的事情。

根据以下规格进行的比较:

HP ProBook Intel Core i5with 4G RAMon Ubuntu 13.04 Linuxwith JDK 的版本:Java HotSpot(TM) 64-Bit Server VM (build 25.0-b23, mixed mode)

我通过这种方式创建了三个字段的自定义对象数组(按保留顺序添加对象):

package com.cmd;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        for (int i=100; i <= 10_000_000; i*=10){
            runTest(i);
        }
    }

    private static void runTest(final int size){

        // Fist obtain two Arrays of same data
        Employee[] empArrForSort = createVeryLargeEmpArray(size);
        Employee[] empArrForSortCopy = Arrays.copyOf(empArrForSort, empArrForSort.length);

        long start = System.currentTimeMillis();
        Arrays.sort(empArrForSort, (e1, e2) -> new Integer(e1.getId()).compareTo(e2.getId()));
        logStart(size + ": sort", start);

        start = System.currentTimeMillis();
        Arrays.parallelSort(empArrForSortCopy, (e1, e2) -> new Integer(e1.getId()).compareTo(e2.getId()));
        logStart(size + ": parallel sort", start);
    }


    private static void logStart(String label, long startTimeMillis) {
        System.out.println("End " + label + " the array. It took: " + (System.currentTimeMillis() - startTimeMillis) + " ms");
    }

    private static Employee[] createVeryLargeEmpArray(final int size) {

        Employee[] ret = new Employee[size];

        for (int i = 0; i < ret.length; i++) {
            ret[i] = Employee.createEmployee(ret.length - i, "Mohammad" + i, "");
        }

        return ret;
    }

    static class Employee {

        private int id;
        private String name;
        private String email;

        private Employee(int id, String name, String email) {
            this.id = id;
            this.name = name;
            this.email = email;
        }

        public static Employee createEmployee(int id, String name, String email) {
            return new Employee(id, name, email);
        }

        public int getId() {
            return id;
        }
    }

}

并且,另一个运行表明,Parallel 仅在列表包含 10,000,000 时执行 pad,在所有其他情况下看起来更好。

>java -Xmx2000m com.cmd.Main
End 100: sort the array. It took: 110 ms
End 100: parallel sort the array. It took: 6 ms
End 1000: sort the array. It took: 2 ms
End 1000: parallel sort the array. It took: 3 ms
End 10000: sort the array. It took: 11 ms
End 10000: parallel sort the array. It took: 11 ms
End 100000: sort the array. It took: 15 ms
End 100000: parallel sort the array. It took: 37 ms
End 1000000: sort the array. It took: 553 ms
End 1000000: parallel sort the array. It took: 187 ms
End 10000000: sort the array. It took: 640 ms
End 10000000: parallel sort the array. It took: 1099 ms
4

3 回答 3

9

这里的重点是数组以相反的顺序排序。这是一个非常独特的场景,并不暗示算法的一般性能。我运行了相同的代码,但使用了无序数组:

  ret[i] = Employee.createEmployee(rnd.nextInt(ret.length), "Mohammad" + i, "");

结果显示,与反向排序相比,性能要慢得多,而 parallelSort 比简单排序要快得多。

End 100: sort the array. It took: 139 ms
End 100: parallel sort the array. It took: 4 ms
End 1000: sort the array. It took: 4 ms
End 1000: parallel sort the array. It took: 6 ms
End 10000: sort the array. It took: 35 ms
End 10000: parallel sort the array. It took: 30 ms
End 100000: sort the array. It took: 420 ms
End 100000: parallel sort the array. It took: 144 ms
End 1000000: sort the array. It took: 1341 ms
End 1000000: parallel sort the array. It took: 506 ms
End 10000000: sort the array. It took: 12200 ms
End 10000000: parallel sort the array. It took: 3971 ms
于 2013-09-09T13:29:42.053 回答
1

仅修改方法中的2行createVeryLargeEmpArray如下。

Random random = new Random();
ret[i] = Employee.createEmployee(ret.length - i+random.nextInt(size), "Mohammad" + i, "");

这是结果

End 100:对数组进行排序。耗时:27 ms
结束 100:对数组进行并行排序。耗时:1 毫秒

End 1000:对数组进行排序。耗时:1 ms
结束 1000:对数组进行并行排序。耗时:1 毫秒

End 10000:对数组进行排序。耗时:7 ms
结束 10000:对数组进行并行排序。耗时:145 毫秒

End 100000:对数组进行排序。耗时:105 ms
结束 100000:对数组进行并行排序。耗时:59 毫秒

End 1000000:对数组进行排序。耗时:1050 ms
结束 1000000:对数组进行并行排序。耗时:194 毫秒

End 10000000:对数组进行排序。耗时:12636 ms
结束 10000000:对数组进行并行排序。耗时:2107 毫秒

随着未排序元素数量的增加,并行排序开始比常规排序执行得更好。

于 2015-07-03T13:42:52.323 回答
-1

我创建了许多测试,并且在大多数情况下,parallelSort 的性能比(串行)排序好得多。

于 2014-06-24T23:27:45.027 回答