Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5031157

(coll) Performance improvement for sorting

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 1.4.2
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:

      Description



      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)
      ======================================================================

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: