《Effective Java Programming Language Guide》 Item 33: Beware the performance of string concatenationThe string concatenation operator (+) is a convenient way to combine a few strings into one. It is fine for generating a single line of output or for constructing the string representation of a small, fixed-size object, but it does not scale. Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n. It is an unfortunate consequence of the fact that strings are immutable (Item 13). When two strings areconcatenated, the contents of both are copied.For example, consider the following method that constructs a string representation of a billing statement by repeatedly concatenating a line for each item:// Inappropriate use of string concatenation - Performs horribly! public String statement() { String s = ""; for (int i = 0; i < numItems(); i++) s += lineForItem(i); // String concatenation return s; }This method performs abysmally if the number of items is large. To achieve acceptableperformance, use a StringBuffer in place of a String to store the statement underconstruction: public String statement() { StringBuffer s = new StringBuffer(numItems() * LINE_WIDTH); for (int i = 0; i < numItems(); i++) s.append(lineForItem(i)); return s.toString(); }The difference in performance is dramatic. If numItems returns 100 and lineForItem returns a constant 80-character string, the second method is ninety times faster on my machine than the first. Because the first method is quadratic in the number of items and the second is linear, the performance difference is even more dramatic for larger numbers of items. Note that the second method preallocates a StringBuffer large enough to hold the result. Even if it is detuned to use a default-sized StringBuffer, it is still forty-five times faster than the first.The moral is simple: Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuffer's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them.
我觉得concat()方法跟String的+运算是等同的
When two strings areconcatenated, the contents of both are copied. String s1="adfasd"; String s2="oiuioqer"; s1=s1+s2; 也就是说s1,s2被拷贝到一块内存区然后合并再赋给一个新的内存区s1(新创建的一个对象)?几位朋友还是未明确说明
你可以看看JDK的文档,说得很详细。
大家都来谈谈 意见~
我知道String不能改变大小,但是String不是也可以用 + 号连接么(是不是这个过程就是创建了新的String对象?)?这个大小到底是什么含义呢?haode(好的) ,大致明白了你的意思。如果只对某些可能发生非常有限的变化的字符串进行构造的时候呢?总觉得StringBuffer写代码的时候显得非常累赘。
这种拷贝过程的代价是相当大的。因此一个基本的性能原则是如果需要构造一个字符串,应明确地使用StringBuffer对象。类似+和+=这样的字符串连接操作偶尔使用是很好的,但是它们的代价是很高的。
实例赋值 this.i=n; 1.2
int增值 i++; 1.5
byte增值 b++; 2.0
short增值 s++; 2.0
float增值 f++; 2.0
double增值 d++; 2.0
空循环 while(true) n++; 2.0
三元表达式 (x<0) ?-x : x 2.2
算术调用 Math.abs(x); 2.5
数组赋值 a[0] = n; 2.7
long增值 l++; 3.5
方法调用 funct(); 5.9
throw或catch异常 try{ throw e; }或catch(e){} 320
同步方法调用 synchMehod(); 570
新建对象 new Object(); 980
新建数组 new int[10]; 3100
字串的开销:字串连接运算符+看似简单,但实际需要消耗大量系统资源。编译器可高效地连接字串,但变量字串却要求可观的处理器时间。例如,假设s和t是字串变量:
System.out.println("heading" + s + "trailer" + t);
上述语句要求新建一个StringBuffer(字串缓冲),追加自变量,然后用toString()将结果转换回一个字串。因此,无论磁盘空间还是处理器时间,都会受到严重消耗。若准备追加多个字串,则可考虑直接使用一个字串缓冲——特别是能在一个循环里重复利用它的时候。通过在每次循环里禁止新建一个字串缓冲,可节省980单位的对象创建时间(如前所述)。利用substring()以及其他字串方法,可进一步地改善性能。如果可行,字符数组的速度甚至能够更快。也要注意由于同步的关系,所以StringTokenizer会造成较大的开销。
那么如果用String对象的concat()方法把字符串拼接起来又如何呢?
这样作的结果是生成一个新的字符串,但原来的字符串不变!
而不能 用String的?????
public String statement() {
String s = "";
for (int i = 0; i < numItems(); i++)
s += lineForItem(i); // String concatenation
return s;
}This method performs abysmally if the number of items is large. To achieve acceptableperformance, use a StringBuffer in place of a String to store the statement underconstruction: public String statement() {
StringBuffer s = new StringBuffer(numItems() * LINE_WIDTH);
for (int i = 0; i < numItems(); i++)
s.append(lineForItem(i));
return s.toString();
}The difference in performance is dramatic. If numItems returns 100 and lineForItem returns a constant 80-character string, the second method is ninety times faster on my machine than the first. Because the first method is quadratic in the number of items and the second is linear, the performance difference is even more dramatic for larger numbers of items. Note that the second method preallocates a StringBuffer large enough to hold the result. Even if it is detuned to use a default-sized StringBuffer, it is still forty-five times faster than the first.The moral is simple: Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuffer's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them.
String s1="adfasd";
String s2="oiuioqer";
s1=s1+s2;
也就是说s1,s2被拷贝到一块内存区然后合并再赋给一个新的内存区s1(新创建的一个对象)?几位朋友还是未明确说明
StringBuffer sb;
String s;
然后让sb循环十万遍加一个字符串
让s循环同样的次数加同样的字符串
然后打印各自用的时间.
你会发现,StringBuffer比String快 成千上万倍 !!!
呵呵,具体是几千还是几万我忘了 :) ------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
StringBuffer s2 = new StringBuffer("");
s2 = s1;于是s2会随着s1的改变而改变,但是String不会===================================
我的一分耕耘,你能给一分收获