Performance Checklist

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

  • Logically partition your strings into those that require internationalization support (i.e., text) and those that don’t.

    • Avoid internationalization where the Strings never require it.

  • Avoid using the StreamTokenizer.

  • Create and optimize your own framework to convert objects and primitives to and from strings.

  • Use efficient methods of String that do not copy the characters of the string, e.g., String.substring().

    • Avoid using inefficient methods of String that copy the characters of the string, e.g., String.toUppercase() and String.toLowercase( ).

    • Use the string concatenation operator to create Strings at compile time.

    • Use StringBuffers to create Strings at runtime.

    • Specify when the underlying char array is copied when reusing StringBuffers.

  • Improve access to the underlying String char array by copying the chars into your own array.

    • Manipulate characters in char arrays rather than using String and StringBuffer manipulation.

    • Reuse char arrays.

  • Optimize the string comparison and search algorithm for the data being compared and searched.

    • Compare strings by identity.

    • Convert a comparison task to a (hash) table lookup.

    • Handle case-insensitive comparisons differently from case-sensitive comparisons.

    • Apply the standard performance optimization for case-insensitive access (maintaining a second collection with all strings uppercased).

    • Use java.text.CollationKeys rather than a java.text.Collator object to sort ...

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.