Dec 20, 2014

Java 8: Arrays.parallelSort()

In this post, we will discuss about new sorting API for Arrays introduced in JAVA 8, Arrays.parallelSort().

Java 8's parallel sort API uses a split-sort-merge concept to sort the arrays. The input array is broken into multiple sub-arrays that are sorted themselves and then finally merged resulting into a complete sorted array. The algorithm uses a threshold value and any array of size lesser than the threshold value is sorted using the appropriate Arrys.sort method. The algorithm requires a working space no greater than the size of the original array.


The method syntax for parallel sort is as follows:
Arrays.parallelSort(myArray);


Below are different overloaded APIs available for parallel sorting:

 01. parallelSort(byte[] a)  
 02. parallelSort(byte[] a,int fromIndex,int toIndex)  
 03. parallelSort(char[] a)  
 04. parallelSort(char[] a,int fromIndex,int toIndex)  
 05. parallelSort(double[] a)  
 06. parallelSort(double[] a,int fromIndex,int toIndex)  
 07. parallelSort(float[] a)  
 08. parallelSort(float[] a,int fromIndex,int toIndex)  
 09. parallelSort(int[] a)  
 10. parallelSort(int[] a,int fromIndex,int toIndex)  
 11. parallelSort(long[] a)  
 12. parallelSort(long[] a,int fromIndex,int toIndex)  
 13. parallelSort(short[] a)  
 14. parallelSort(short[] a,int fromIndex,int toIndex)  
 15. parallelSort(T[] a)  
 16. parallelSort(T[] a,Comparator<?super T> c)  
 17. parallelSort(T[] a,int fromIndex,int toIndex)  
 18. parallelSort(T[] a,int fromIndex,int toIndex,Comparator<?super T> c)  

Lets create a program to test and compare the efficiency of Arrays.parrallelSort() and Arrays.sort(). What below program does is simply creates an array (and clone of it) of specific size and fill in with random numbers. Two clones are then sorted using both APIs.

 import java.util.Arrays;  
 import java.util.Random;  
 public class ParallelSortExample {  
      public static void main(String[] args) {  
           int size = 100000000;  
           int[] a = new int[size];  
           int[] a1 = new int[size];  
           int maximum = size;  
           int minimum = 1;  
           int range = maximum - minimum + 1;  
           for (int i = 0; i < size; i++) {  
                Random rn = new Random();  
                int randomNum = rn.nextInt(range) + minimum;  
                a[i] = randomNum;  
           }  
           a1 = a.clone();  
           long currentTimeMillis = System.currentTimeMillis();   
           Arrays.parallelSort(a);  
           System.out.println("Arrays.parallelSort time (in millisecond) " + (System.currentTimeMillis() - currentTimeMillis));  
           long currentTimeMillis1 = System.currentTimeMillis(); 
           Arrays.sort(a1);  
           System.out.println("Arrays.sort time (in millisecond) " + (System.currentTimeMillis() - currentTimeMillis1));  
      }  
 }  

Output:
 Arrays.parallelSort time (in millisecond) 4395  
 Arrays.sort time (in millisecond) 9173  

See how fast the sorting is in case of extremely large arrays. But in case of small arrays, the overhead of parallel sorting is extremely large.

Change the size of array to 10000 and execute the program again and see the sorting time again.
 Arrays.parallelSort time (in millisecond) 176  
 Arrays.sort time (in millisecond) 1  

As you can see, overhead is extremely large for small size arrays.

General strategy before opting for either Arrays.parallelSort() or Arrays.sort() API would be think how many elements are expected in the array and then do a little performance testing on the original data-set.

Hope you liked the post. Follow us here to stay updated.

0 comments:

Post a Comment