`
tuhaitao
  • 浏览: 379306 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

String、StringBuffer与StringBuilder

    博客分类:
  • java
阅读更多
先不废话了,来段程序:
package com.tuz;

public class StringTestDemo {

	public static final int count = 10000;

	public static long StringTest(String str) {
		long start = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			str += i;
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	public static long StringTest(StringBuffer strBuffer) {
		long start = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			strBuffer.append(i);
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	public static long StringTest(StringBuilder strBuilder) {
		long start = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			strBuilder.append(i);
		}
		long end = System.currentTimeMillis();
		return end - start;
	}

	public static void main(String[] args) {
		String str = new String("");
		StringBuffer strBuffer = new StringBuffer("");
		StringBuilder strBuilder = new StringBuilder("");
		
		System.out.println("String cost:" + StringTest(str));
		System.out.println("StringBuffer cost:" + StringTest(strBuffer));
		System.out.println("StringBuilder cost:" + StringTest(strBuilder));

	}

}

当count的值为1000的时候输出如下:
String cost:453
StringBuffer cost:0
StringBuilder cost:0
此时可见StringBuffer与StringBuilder在字符串连接的性能远远超出了String,为了找出性能最好的到底是那个,修改cont为1000000,注释掉"String cost",因为这行的运行速度太慢了,结果:
StringBuffer cost:172
StringBuilder cost:125
好了,结果出来了,在可变字符串中,String < StringBuffer < StringBuilder

关于讲String的文章太多了,这里就不多少了,下边看下StringBuffer 与 StringBuilder到底StringBuilder比StringBuffer 快在什么地方,我们还是打开jdk6.0下的src.zip源代码包,看看:
首先看看StringBuffer.java文件头部的注释:
我只截取了一段
/**
 * A thread-safe, mutable sequence of characters. 
 * A string buffer is like a {@link String}, but can be modified. At any 
 * point in time it contains some particular sequence of characters, but 
 * the length and content of the sequence can be changed through certain 
 * method calls.
 */

呵呵,看到了吧,thread-safe这个词那么StringBuilder可能不是线程安全的,
我们迅速打开StringBuilder.java来看下:
/**
 * A mutable sequence of characters.  This class provides an API compatible
 * with <code>StringBuffer</code>, but with no guarantee of synchronization.
 * This class is designed for use as a drop-in replacement for
 * <code>StringBuffer</code> in places where the string buffer was being
 * used by a single thread (as is generally the case).   Where possible,
 * it is recommended that this class be used in preference to
 * <code>StringBuffer</code> as it will be faster under most implementations.
 */

上边也说了,这个类提供了与StringBuffer并存的API,但是不保证同步,意思很明显了,即这个类非线程安全,具体的再看下那个append方法:
StringBuffer.java
    /**
     * @see     java.lang.String#valueOf(int)
     * @see     #append(java.lang.String)
     */
    public synchronized StringBuffer append(int i) {
	super.append(i);
        return this;
    }


StringBuilder.java
    /**
     * @see     java.lang.String#valueOf(int)
     * @see     #append(java.lang.String)
     */
    public StringBuilder append(int i) {
	super.append(i);
        return this;
    }

以为同步的时候需要耗费一定的资源与时间所以,如果在不是多线程的情况下,建议大家使用StringBuilder,最大化的提高我们的编码质量,因为我们是程序员,呵呵,本人水平有限,有什么不对的地方请大家斧正。
3
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics