Difference between Arrays.sort() and Arrays.parallelSort()
正在经历
并行排序使用线程-每个线程都获得列表的一个块,并且所有块均以并行方式对其进行排序。然后将这些分类的块合并为一个结果。
当集合中包含很多元素时,它会更快。在较大的集合上,并行化(拆分成块并合并)的开销可以容忍地变小,但是对于较小的集合,则是很大的。
看一下这张表(当然,结果取决于CPU,内核数,后台进程等):
取自此链接:http://www.javacodegeeks.com/2013/04/arrays-sort-versus-arrays-parallelsort.html
Arrays.parallelSort():
The method uses a threshold value and any array of size lesser than the threshold value is sorted using the Arrays#sort() API (i.e sequential sorting). And the threshold is calculated considering the parallelism of the machine, size of the array and is calculated as:
1 2 3 4 5 | private static final int getSplitThreshold(int n) { int p = ForkJoinPool.getCommonPoolParallelism(); int t = (p > 1) ? (1 + n / (p << 3)) : n; return t < MIN_ARRAY_SORT_GRAN ? MIN_ARRAY_SORT_GRAN : t; } |
Once its decided whether to sort the array in parallel or in serial, its now to decide how to divide the array in to multiple parts and then assign each part to a Fork/Join task which will take care of sorting it and then another Fork/Join task which will take care of merging the sorted arrays. The implementation in JDK 8 uses this approach:
Divide the array into 4 parts.
Sort the first two parts and then merge them.
Sort the next two parts and then merge them.
And the above steps are repeated recursively with each part until the size of the part to sort is not lesser than the threshold value calculated above.
您还可以阅读Javadoc中的实现细节
The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort method. The algorithm requires a working space no greater than the size of the specified range of the original array. The ForkJoin common pool is used to execute any parallel tasks.
Array.sort():
This uses merge sort OR Tim Sort underneath to sort the contents. This is all done sequentially, even though merge sort uses divide and conquer technique, its all done sequentially.
资源
两种算法之间的主要区别如下:
1. Arrays.sort():是一种顺序排序。
- API使用单线程进行操作。
- API需要更长的时间来执行操作。
2. Arrays.ParallelSort():是并行排序。
该API使用多个线程。
- 与Sort()相比,该API花费的时间更少。
要获得更多结果,我们都必须等待JAVA 8!欢呼!
您可以参考javadoc,它解释了如果数组足够大,该算法将使用多个线程:
The sorting algorithm is a parallel sort-merge that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate
Arrays.sort method. [...] TheForkJoin common pool is used to execute any parallel tasks.
从这个连结
Current sorting implementations provided by the Java Collections
Framework (Collections.sort and Arrays.sort) all perform the sorting
operation sequentially in the calling thread. This enhancement will
offer the same set of sorting operations currently provided by the
Arrays class, but with a parallel implementation that utilizes the
Fork/Join framework. These new API’s are still synchronous with regard
to the calling thread as it will not proceed past the sorting
operation until the parallel sort is complete.
简而言之,
您现在可以使用–
这将自动将目标集合分为几个部分,这些部分将在多个核心之间进行独立排序,然后重新组合在一起。唯一需要注意的是,当在繁忙的Web容器等高度多线程的环境中调用该方法时,由于增加了CPU上下文切换的成本,这种方法的优势将开始减少(减少90%以上)。
源链接