java String拼接的方法选择及性能分析
String 拼接的方法选择
在拼接静态字符串时,尽量用 +,因为通常编译器会对此做优化,如:
String test = "this " + "is " + "a " + "test " + "string"
编译器会把它视为:
String test = "this is a test string"
在拼接动态字符串时,尽量用 StringBuffer
或 StringBuilder
的 append
,这样可以减少构造过多的临时 String 对象。
测试代码:(按照附录1修改)
public class teststring { public void testPlus() {
String s = "";
long ts = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s = s + String.valueOf(i);
}
long te = System.currentTimeMillis();
System.out.println("+ cost {" + ( te - ts) + "} ms");
} public void testConcat() {
String s = "";
long ts = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s = s.concat(String.valueOf(i));
}
long te = System.currentTimeMillis();
System.out.println("concat cost {" + (te - ts) + "} ms");
} public void testStringBuffer() {
StringBuffer sb = new StringBuffer();
long ts = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append(String.valueOf(i));
}
sb.toString();
long te = System.currentTimeMillis();
System.out.println("StringBuffer cost {" + (te - ts) + "} ms");
} public void testStringBuilder() {
StringBuilder sb = new StringBuilder();
long ts = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
sb.append(String.valueOf(i));
}
sb.toString();
long te = System.currentTimeMillis();
System.out.println("StringBuilder cost {" + (te - ts) + "} ms");
} public static void main(String[] args) {
teststring a = new teststring();
a.testConcat();
a.testPlus();
a.testStringBuffer();
a.testStringBuilder();
}
}
运行结果:
concat cost {} ms 113
+ cost {} ms 195
StringBuffer cost {} ms 2
StringBuilder cost {} ms 9
可见 存在大量的拼接动态字符串操作时,尽量用 StringBuffer
或 StringBuilder
的 append。使用'+'运算符的开销是不可忍受的。
In general, if sb refers to an instance of a StringBuilder
, then sb.append(x)
has the same effect as sb.insert(sb.length(), x)
. Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.
Instances of StringBuilder
are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer
be used.
附录:
1 Java 5种字符串拼接方式性能比较 http://blog.csdn.net/kimsoft/article/details/3353849
2 Java 性能优化之 String 篇 http://www.ibm.com/developerworks/cn/java/j-lo-optmizestring/
最新文章
- 消息服务MNS和消息队列ONS产品对比
- UVA - 10375 Choose and divide[唯一分解定理]
- [BZOJ 2819]Nim
- POJ3020Antenna Placement(最小路径覆盖+重在构图)
- [LeetCode]题解(python):040-Combination Sum II
- ubuntu下配置SVN服务器
- MyEclipse下搭建maven项目
- Java设计模式之装饰者模式
- 【IE6的疯狂之七】样式中文注释后引发失效
- bzoj1336: [Balkan2002]Alien最小圆覆盖
- 安卓OpenGL入门
- Human Motion Analysis with Wearable Inertial Sensors——阅读1
- nginx location详解
- session和token
- php curl POST multipart/form-data与application/x-www-form-urlencode的区别
- Codeforces Round #544 (Div. 3) (补)
- JavaSE——TCP协议网络编程(一)
- 深入理解VMware虚拟机网络通信原理
- [Java.web]简单计算器
- hadoop源代码组织结构与阅读技巧