1.

How Do You Optimize Strings?

Answer»

• Use StringBuffer instead of STRING concat '+' operator
• Formatting NUMBERS using java.text.DecimalFormat is ALWAYS SLOWER than Double.toString(double) method, because internally java.text.DecimalFormat() CALLS Double.toString(double) then parses and converts the results.
• Convert String to char[] arrays to process characters rather than accessing one at a time using String.charAt() method
• Creating Double from string is slow
• Intern() Strings to enable (==) comparisions
• Use char arrays for all character processing in loops, rather than using String or StringBuffer classes
• Set the initial string buffer size to maximum if it known.
• StringTokenizer is very inefficient, and can be optimized by storing the string and delimiter in a character array instead of in String.
• DON'T create static strings via new().
• Where the compiler cannot resolve concatenated strings at compile time, the code should be converted to StringBuffer appends, and the StringBuffer should be appropriately sized rather than using the default size.
• The compiler concatenates strings where they are fully resolvable, so don t move these concatenations to runtime with StringBuffer.

• Use StringBuffer instead of String concat '+' operator
• Formatting numbers using java.text.DecimalFormat is always slower than Double.toString(double) method, because internally java.text.DecimalFormat() calls Double.toString(double) then parses and converts the results.
• Convert String to char[] arrays to process characters rather than accessing one at a time using String.charAt() method
• Creating Double from string is slow
• Intern() Strings to enable (==) comparisions
• Use char arrays for all character processing in loops, rather than using String or StringBuffer classes
• Set the initial string buffer size to maximum if it known.
• StringTokenizer is very inefficient, and can be optimized by storing the string and delimiter in a character array instead of in String.
• DON'T create static strings via new().
• Where the compiler cannot resolve concatenated strings at compile time, the code should be converted to StringBuffer appends, and the StringBuffer should be appropriately sized rather than using the default size.
• The compiler concatenates strings where they are fully resolvable, so don t move these concatenations to runtime with StringBuffer.



Discussion

No Comment Found