string,stringbuffer,stringbuilder

1730 단어 StringBuilder
stringbuilder > stringbuffer > string
테스트 용례 코드는 다음과 같다.
/**
 * fileName:Test01.java
 * author:cuiyaonan email:[email protected]
 * createDate:2012-12-24 
*/
package test_string;

import java.util.Random;

public class Test01 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/**
		 * 
		 * author:cuiyaonan2000 
		 */
		  Random random = new Random(1000);
		//0
		long a = System.currentTimeMillis();
		String a1 = new String("a");
		for(int i = 0 ; i < 10000 ; i++){
			a1+=random.nextFloat();
		}
		long b = System.currentTimeMillis();
		System.out.println("string:" + ( b-a ));
//		System.out.println(a1);
		
		// 1
		long c = System.currentTimeMillis();
		StringBuffer c1 = new StringBuffer("a");
		for(int i = 0 ; i < 10000 ; i++){
			c1.append(random.nextFloat());
		}
		long d = System.currentTimeMillis();
		System.out.println("stringbuffer:"+(d-c));
//		System.out.println(c1);
		
		// 2
		long e = System.currentTimeMillis();
		StringBuilder e1 = new StringBuilder("a");
		for(int i = 0 ; i < 10000 ; i++){
			e1.append(random.nextFloat());
		}
		long f = System.currentTimeMillis();
		System.out.println("stringbuild:"+(f-e));
//		System.out.println(e1);

	}

}

결과는 다음과 같습니다.

string:1343
stringbuffer:16
stringbuild:0

좋은 웹페이지 즐겨찾기