I was reading about the new features in Java 8 and one of them was the new Arrays.parallelSort() method. I made some tests sorting an array of doubles and one of Strings and for Strings the parallelSort was much slower.
Here is the content of a test method for Strings:
final int size = 10000;
final String[] values1 = new String[size];
final String[] values2 = new String[size];
for (int i = 0; i < size; i++) {
values1[i] = Integer.toString(i);
values2[i] = values1[i];
}
Collections.shuffle(Arrays.asList(values1));
Collections.shuffle(Arrays.asList(values2));
final Comparator<String> comparator = (o1, o2) -> o2.compareTo(o1);
long startTimeInNano = System.nanoTime();
Arrays.sort(values1, comparator);
long endTimeInNano = System.nanoTime();
System.out.println("Arrays.sort: totalTimeInMicro= " + ((endTimeInNano - startTimeInNano)/1000));
//parallel sort with java 8
startTimeInNano = System.nanoTime();
Arrays.parallelSort(values2,comparator);
endTimeInNano = System.nanoTime();
System.out.println("Arrays.parallelSort: totalTimeInMicro= " + ((endTimeInNano - startTimeInNano)/1000));
The result was:
Arrays.sort: totalTimeInMicro= 11993
Arrays.parallelSort: totalTimeInMicro= 89823
I also tried this code on another computer and the result was the same (25608 vs 808660). The computer I run the tests has an i5-2500 CPU. Do you have any idea why I get this kind of results?