Performance Checklist

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

  • Test using either the target size for collections or, if this is not definite, various sizes of collections.

  • Test updating collections using the expected order of the data or, if this is not definite, various orders of data, including sorted data.

  • Match the scaling characteristics of the structures against the volumes of data likely to be applied.

  • Presize collections to their final sizes when possible.

  • Consider switching to alternative data structures or algorithms.

    • Use the most appropriate collection class available.

    • Consider using two collections with different performance characteristics to hold the same data.

    • Consider using plain arrays, e.g., int[], Object[].

    • Consider using hybrid data structures.

    • Use specialized collections that avoid casts or unnecessary method calls.

    • Consider wrapping the elements of the collection in a specialized class that improves access times (e.g., Hashtable key class with faster hashCode( ) and equals( ) methods).

    • Add caching or specialized accessors to collections where some elements are accessed more often than others.

  • Access the underlying collection structure when iterating over the elements of a collection.

    • Copy the elements into an array rather than access many elements one at a time through the collection element accessors.

  • Preallocate memory for element storage rather than allocating at update time.

    • Reuse element-storage objects when they are released.

Get Java Performance Tuning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.