Effective Java 51 Beware the performance of string concatenation
2014-04-14 10:47
531 查看
Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n. It's an unfortunate consequence of fact that strings are immutable(Item 15).
// Inappropriate use of string concatenation - Performs horribly!
public String statement() {
String result = "";
for (int i = 0; i < numItems(); i++)
result += lineForItem(i); // String concatenation
return result;
}
To achieve acceptable performance, use a StringBuilder in place of a String
public String statement() {
StringBuilder b = new StringBuilder(numItems() * LINE_WIDTH);
for (int i = 0; i < numItems(); i++)
b.append(lineForItem(i));
return b.toString();
}
Summary
Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuilder's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them.
// Inappropriate use of string concatenation - Performs horribly!
public String statement() {
String result = "";
for (int i = 0; i < numItems(); i++)
result += lineForItem(i); // String concatenation
return result;
}
To achieve acceptable performance, use a StringBuilder in place of a String
public String statement() {
StringBuilder b = new StringBuilder(numItems() * LINE_WIDTH);
for (int i = 0; i < numItems(); i++)
b.append(lineForItem(i));
return b.toString();
}
Summary
Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuilder's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them.
相关文章推荐
- Java String vs StringBuilder vs StringBuffer Concatenation Performance Micro Benchmark
- "String Concatenation vs String Builder - The performance hit! See it to believe it :o)"
- Java String Concatenation and Performance(String串联4个操作性能对比)
- The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List
- The Principles of Java Application Performance Tuning
- Educational Codeforces Round 9 C - The Smallest String Concatenation ,学习到string
- The Smallest String Concatenation
- (Java) LeetCode 30. Substring with Concatenation of All Words —— 与所有单词相关联的字串
- Is the Improvement of String.substring() in Java7 Really Reasonable?
- Educational Codeforces Round 9-C. The Smallest String Concatenation(字符串排序)
- The Smallest String Concatenation CodeForces - 632C 思维
- 【leetcode】String——Substring with Concatenation of All Words(30)
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- [leetcode]30. Substring with Concatenation of All Words@Java
- the format string of java
- the evil cmake -- string concatenation
- 30. Substring with Concatenation of All Words (String, Map)
- Substring with Concatenation of All Words leetcode java
- (Java)LeetCode-30. Substring with Concatenation of All Words
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序