Arrays.sort and Arrays.parallelSort Function Behavior

I have the following code,

import java.util.Arrays; public class ParellelStream { public static void main(String args[]){ Double dbl[] = new Double[1000000]; for(int i=0; i<dbl.length;i++){ dbl[i]=Math.random(); } long start = System.currentTimeMillis(); Arrays.parallelSort(dbl); System.out.println("time taken :"+((System.currentTimeMillis())-start)); } } 

When I run this code, it takes from 700 to 800 ms, but when I replace the Arrays.parallelSort string with Array.sort, it takes from 500 to 600 ms. I read about the Arrays.parallelSort and Arrays.sort methods, which say that Arrays.parellelSort gives poor performance when the data set is small, but here I use an array of 1,000,000 elements. what could be causing concurrent poor performance? I am using java8.

+5
source share
2 answers

I read about the Array.parallelSort and Arrays.sort array methods, which states: that Arrays.parellelSort gives poor performance when the data set is small but here I use an array of 1,000,000 elements.

This is not the only thing to consider. It depends on your computer (how your processor handles multithreading, etc.).

Here is a quote from the Parallelism part of the Java Tutorials

Please note that Parallelism is not automatically faster than running, although it can be, if you have enough data and processor cores [...], you are still responsible for your application suitable for parallelism.

You can also look at java.util.ArraysParallelSortHelpers code for a better understanding of the algorithm.

Note that the parallelSort method uses the ForkJoinPool introduced in Java 7 to take advantage of each processor on your computer, as stated in javadoc:

A ForkJoinPool is built with a given Parallelism target level; by default, equal to the number of processors available.

Note that if the length of the array is less than 1 << 13 , the array will be sorted using the appropriate Arrays.sort method.

see also

+4
source

The parallelSort function will use a thread for each processor core that you have on your computer. In particular, parallelSort runs tasks in the ForkJoin shared thread pool. If you have only one core, you will not see an improvement over single-threaded sorting.

If you have only a few cores, you will have some initial cost associated with creating new threads, which will mean that for relatively small arrays you will not see a linear increase in performance.

The comparison function for comparing doubles is not an expensive function. I think that in this case, 1,000,000 elements can be safely considered small, and the benefits of using multiple threads are outweighed by the initial costs of creating these threads. Since the initial costs will be fixed, you should see a performance gain when using large arrays.

+5
source

All Articles