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
String
s 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()
andString.toLowercase( )
.Use the string concatenation operator to create
String
s at compile time.Use
StringBuffer
s to createString
s at runtime.Specify when the underlying
char
array is copied when reusingStringBuffer
s.
Improve access to the underlying
String
char
array by copying thechar
s into your own array.Manipulate characters in
char
arrays rather than usingString
andStringBuffer
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.CollationKey
s rather than ajava.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.