在日常的开发工作,特别是在Dao层类里面写SQL的时候,经常看到有人通过创建一个String对象,然后不断用+号链接。如:
String sql = "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort";
经常听到有同事在讨论String和StringBuffer之间的区别,百度上也有很多程序侠们发表过二者的区别,甚至通过代码来证明两者的时间消耗差距,但都只是在讨论String 与 StringBuffer的差异,其实StringBuffer的使用还有另外一种情况需要比较和注意,百度上也没见有人做过这类的研究。就是在StringBuffer的append内部使用+号的情况与每次.append的的区别和性能差异。如:
//第1种 多次通过append连接
StringBuffer sb = new StringBuffer();
sb.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort");
//第2种 在append内部使用+号
StringBuffer sb = new StringBuffer();
sb.append("We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort");
为了了解String、StringBuffer,以及StringBuffer的append和append内部使用+号这三种使用方式性能消耗情况,我通过代码实验在同等条件下最终得到以下结果:
使用string的时间是:39023毫秒!
使用StringBuffer的时间是:10毫秒!
使用StringBuffer内+号的时间是:6毫秒!
通过上述实验结果,可以看出StringBuffer比String性能好,而StringBuffer的两种用法中,内部+号性能最好。其实看过Java底层代码编译源码的应该知道,在编译过程中String对象其实最终还是通过创建StringBuffer对象,String每次+操作都会new一个StringBuffer来进行拼接,所以在这个过程中+的越多,new的StringBuffer对象就越多。而在StringBuffer内部使用+号连接在编译过程中,其实是不会多次创建StringBuffer对象的。
关于它们的区别我这里就不做深入介绍了,比较详细的深入的介绍网上一堆,在这里只是想通过实验来告诉程序侠们它们在用法上的性能区别有多大。因为每种用法其实需要结合你的使用场景来决定的,如果涉及大量的字符拼接的使用StringBuffer性能是最好的也建议你这样操作。
以下是实验代码:
public static void main(String[] args) {
String str = "";
StringBuffer sb = new StringBuffer();
long start = 0L;
long end = 0L;
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
str += "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort";
}
end = System.currentTimeMillis();
System.out.println("使用string的时间是:" + (end - start) + "毫秒!");
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
sb.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort")
.append("We always knew our daughter Kendall was going be a performer of some sort");
}
end = System.currentTimeMillis();
System.out.println("使用StringBuffer的时间是:" + (end - start) + "毫秒!");
start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
sb.append("We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort"
+ "We always knew our daughter Kendall was going be a performer of some sort");
}
end = System.currentTimeMillis();
System.out.println("使用StringBuffer内+号的时间是:" + (end - start) + "毫秒!");
}