import java.io.*;
import java.util.*;
import java.util.Arrays;
public class Parallel_Sort
{
public static void main(String[] args)
{
// Create a list and populate it with
random numbers
List<Integer> list = new
ArrayList();
Random r = new Random();
for (int x = 0; x < 500000; x++)
{
list.add(r.nextInt(10000));
}
// Create two arrays for sorting.
Integer[] array1 = new
Integer[list.size()];
Integer[] array2 = new
Integer[list.size()];
array1 = list.toArray(array1);
array2 = list.toArray(array2);
// sort array
long start = System.currentTimeMillis();
Arrays.sort(array1);
long end = System.currentTimeMillis();
double ss = (double)(end - start)/1000;
System.out.println("Sort
Time(Sequential): " + ss);
// sort array using parallel sort
start = System.currentTimeMillis();
Arrays.parallelSort(array2);
end = System.currentTimeMillis();
double ps = (double)(end - start)/1000;
System.out.println("Parallel Sort
Time(Parallel): " + ps);
System.out.println("Time Difference
is: " + (double)(ss-ps));
}
}
OUTPUT:
C:\Users\java>java Parallel_Sort
Sort Time(Sequential): 0.141
Parallel Sort Time(Parallel): 0.078
Time Difference is: 0.06299999999999999
0 comments:
Post a Comment