9.4 Performance Checklist

Most of these suggestions apply only after a bottleneck has been identified:

  • Eliminate casts in the sorting method.

  • Reimplement a standard sort (such as quicksort) directly in the class being sorted.

  • Make the comparison method faster.

  • Directly access fields rather than calling methods.

  • Sort linked lists with a merge sort.

  • Use comparison keys to replace objects where the comparison method requires a calculation for each object being compared, and that calculation could be cached.

  • Partially presort the array with a faster partial sort; then re-sort using the full comparison method.

  • Use sorting interfaces to support different sorting algorithms.

  • Support generic optimizations within a sorting framework. These optimizations include:

    • Comparison key objects that cache calculations that would otherwise need to be repeatedly executed

    • Comparison key objects that hold the ordering value in a directly accessible public field

    • Improved object creation by mapping arrays of comparison key objects into multiple arrays

  • Use specialized sorting algorithms for faster times and better scaling behavior.

  • Use specialized sorting algorithms when the sorting order of objects can be mapped directly to integer keys.