-
Enhancement
-
Resolution: Unresolved
-
P4
-
1.4.2
-
Fix Understood
-
generic
-
generic
Name: gm110360 Date: 04/13/2004
A DESCRIPTION OF THE REQUEST :
Average performance gains of 5-10% for Arrays.sort(Object[]) and Collections.sort(List) are achievable by reducing the number of "Comparable"- typecasts used.
The proposed change maintains the original interface & semantics, and introduces only minimal code changes (cp. sample below with original implementation).
For a brief newsgroup discussion of this issue search for "Performance Improvement for Sorting", in comp.lang.java.programmer.
JUSTIFICATION :
this is a performance enhancement request / recommendation only, and as such "nice, but not strictly necessary"
SOURCE CODE:
---------- BEGIN SOURCE ----------
import java.util.List;
import java.util.ListIterator;
public final class SortWithComparables {
public static void sort(List list) {
Object[] a = null;
try {
a = list.toArray(new Comparable[0]);
} catch (ArrayStoreException e) {
throw new ClassCastException();
}
sort(a);
ListIterator i = list.listIterator();
for (int j = 0, n = a.length; j < n; j++) {
i.next(); i.set(a[j]);
}
}
public static void sort(Object[] a) {
mergeSort((Comparable[])a.clone(), (Comparable[])a, 0, a.length, 0);
}
private static void mergeSort(Comparable src[], Comparable dest[], int low, int high, int off) {
// like original in Arrays.mergeSort(...), but without any Comparable[]-casts)
}
}
---------- END SOURCE ----------
(Incident Review ID: 249779)
======================================================================