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.

附录:

Java 5种字符串拼接方式性能比较 http://blog.csdn.net/kimsoft/article/details/3353849

2 Java 性能优化之 String 篇  http://www.ibm.com/developerworks/cn/java/j-lo-optmizestring/

最新文章

  1. 消息服务MNS和消息队列ONS产品对比
  2. UVA - 10375 Choose and divide[唯一分解定理]
  3. [BZOJ 2819]Nim
  4. POJ3020Antenna Placement(最小路径覆盖+重在构图)
  5. [LeetCode]题解(python):040-Combination Sum II
  6. ubuntu下配置SVN服务器
  7. MyEclipse下搭建maven项目
  8. Java设计模式之装饰者模式
  9. 【IE6的疯狂之七】样式中文注释后引发失效
  10. bzoj1336: [Balkan2002]Alien最小圆覆盖
  11. 安卓OpenGL入门
  12. Human Motion Analysis with Wearable Inertial Sensors——阅读1
  13. nginx location详解
  14. session和token
  15. php curl POST multipart/form-data与application/x-www-form-urlencode的区别
  16. Codeforces Round #544 (Div. 3) (补)
  17. JavaSE——TCP协议网络编程(一)
  18. 深入理解VMware虚拟机网络通信原理
  19. [Java.web]简单计算器
  20. hadoop源代码组织结构与阅读技巧

热门文章

  1. nodemon 的坑
  2. MySQL 时间函数加减计算
  3. 由浅到深理解ROS(5)- launch启动文件的理解与编写
  4. sqlserver镜像相关资料
  5. eclipse添加web项目报错“Target runtime Apache Tomcat v7.0 is not defined.”
  6. 读取mysq数据库l数据,并使用dataview显示
  7. code2039 骑马修栏杆
  8. Python将阿拉伯数字转化为中文大写-乾颐堂
  9. [GO]设备文件的使用
  10. toolbox类