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/
java String拼接的方法选择及性能分析的更多相关文章
- java String 中 intern方法的概念
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- Java 字符串拼接四种方式的性能比较分析
一.简单介绍 编写代码过程中,使用"+"和"contact"比较普遍,但是它们都不能满足大数据量的处理,一般情况下有一下四种方法处理字符串拼接,如下: 1. 加 ...
- java String的intern()方法
intern()方法用于将字符串对象加入常量池中. public native String intern(); intern()方法返回的是一个常量池中的String对象(即常量池中某个String ...
- java String 提供的方法
String类的判断功能: * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写 * boolean equalsIgnoreCase(String str) ...
- 十大基础排序算法[java源码+动静双图解析+性能分析]
一.概述 作为一个合格的程序员,算法是必备技能,特此总结十大基础排序算法.java版源码实现,强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步: ...
- 30天C#基础巩固------this,base,string中的方法,StringBuilder性能
这里主要是记录下自己学习笔记,希望有个地方在以后可以看到自己走过的路. 关于之前多态的知识有一个口诀,很好理解里面的override和new,virtual关键字. "new则隐藏,over ...
- java String类 trim() 方法源码分析
public String trim() { int arg0 = this.value.length; //得到此字符串的长度 int arg1 = 0; //声 ...
- java String的各种方法及操作
No. 方法名称 功能 字符与字符串 01 public String(char[] value) 将字符数组中所有内容变为字符串 02 public String(char[] value,int ...
- java.String中的方法
(String) str.trim() 该方法返回一个复制该字符串的开头和结尾的白色空格去掉,或字符串,如果它没有头或尾空白. (Boolean) str.contains(str1) 判断 str ...
随机推荐
- 【hdu6148】Valley Numer【数位dp模板题】
题意 对于每组数据给出一个整数n(length(n)<=100),找出不大于n的数字中有多少是Valley Numer.对于Valley的定义是它每一位的数字要么是递增,要么是递减,要么是先递减 ...
- [hdu1823]Luck and Love(二维线段树)
解题关键:二维线段树模板题(单点修改.查询max) #include<cstdio> #include<cstring> #include<algorithm> # ...
- 解剖Nginx·自动脚本篇(3)源码相关变量脚本 auto/sources
在configure脚本中,运行完auto/options和auto/init脚本后,接下来就运行auto/soures脚本.这个脚本是为编译做准备的. 目录 核心模块 事件模块 OpenSSL 模块 ...
- IDEA05 mybatis插件之MyBatisCodeHelper-Pro
前提准备: >IDEA专业版本 1 安装MyBatisCodeHelper-Pro IDEA提供了插件安装功能,可以根据开发需要安装适合的插件 >help -> find actio ...
- RGB直方图与UV直方图
------------------------------------------------------------------------------------ from skimage im ...
- Oracle表格字段采用sequence进行自增长时,采用Dbutils进行insert或update数据时的处理技巧
// 定义插入记录的方法 public Teacher insert(String name, String gender, Double score) { // 获得连接 Connection co ...
- 浅谈c/c++中的指针问题
首先给出几种指针类型来作出区分,不看后面的解析如果可以自己分辨正确那么就算对指针有一个很好的掌握了,就没有必要再去看后面的解析,如果不能完全区分,那么就有必要仔细看看后面解析. 1 Char * p ...
- java 将一个数组中的值按逆序重新存放,例如,原来顺序为:9,5,7,4,8,要求改为:8,4,7, 5,9。
public class Test3 { public static void main(String[] args) { int[] grade = {87,88,89,98,78}; int m; ...
- jQuery中deferred对象的使用(二)
接上一回的内容,漏了一个always()方法,参数也是回调函数,与done和fail不同的是,无论任何情况都执行always方法中的回调. deferred对象的使用(二) deferred对象不光可 ...
- python 中面向对象编程简单总结2
1.python中继承的特点: (1)总是从一个类继承,默认为object类 (2)不要忘记调用super.__init__方法来初始化父类的方法 def __init__(self,args): s ...