String,StringBuffer和StringBuilder之间的区别,速度比

String,StringBuffer和StringBuilder三者之间的区别在下面的注释里面说的很清楚了,下面的这个类是用来测试三者之间的读写速度

得到的结果是StringBuilder > StringBuffer >> String

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class String_StringBuffer_StringBuilder_SpeedTest {  
	  
    public static final int COUNT = 50000;
    
    /**
     * String:字符串常量
     * StringBuffer:字符创变量
     * StringBuilder:字符创变量
     * 
     * String和StringBuffer主要有2个区别:
	 * (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,
	 *    释放原String对象,StringBuffer类对象为可修改对象,可以通过append()方法来修改值
	 * (2)String类对象的性能远不如StringBuffer类
	 * 
     * StringBuilder与 StringBuffer
     * StringBuilder:线程非安全的,但读写速度比StringBuffer快
     * StringBuffer:线程安全的
     * 
     * 
     * */
    
    /** 
     * 执行一项String赋值测试 
     */  
    public static long doStringTest() {  
  
        String str = new String("");  
        long starttime = System.currentTimeMillis();  
        for (int i = 0; i < COUNT; i++) {  
            str = str + "miss";  
        }  
        long endtime = System.currentTimeMillis();  
        System.out.println( "运行耗时:"+(endtime - starttime)+"ms"+"  单线程:执行"+COUNT+"次 str += 操作 ");  
        return (endtime - starttime);
    }  
  
    /** 
     * 执行一项StringBuffer赋值测试 
     */  
    public static long doStringBufferTest() {  
  
        StringBuffer sb = new StringBuffer("");  
        long starttime = System.currentTimeMillis();  
        for (int i = 0; i < COUNT; i++) {  
            sb = sb.append("miss");  
        }  
        long endtime = System.currentTimeMillis();  
        System.out.println( "运行耗时:"+(endtime - starttime)+"ms"+"  单线程:执行"+COUNT+"次 StringBuffer.appeng() 操作 "); 
        return (endtime - starttime);
    }  
  
    /** 
     * 执行一项StringBuilder赋值测试 
     */  
    public static long doStringBuilderTest() {  
  
        StringBuilder sb = new StringBuilder("");  
        long starttime = System.currentTimeMillis();  
        for (int i = 0; i < COUNT; i++) {  
            sb = sb.append("miss");  
        }  
        long endtime = System.currentTimeMillis();  
        System.out.println( "运行耗时:"+(endtime - starttime)+"ms"+"  单线程:执行"+COUNT+"次 StringBuilder.appeng() 操作 "); 
        return (endtime - starttime);
    }  
  
    /** 
     * 测试StringBuffer遍历赋值结果 
     *  
     * @param mlist 
     */  
    public static long doStringBufferListTest(List mlist) {  
        StringBuffer sb = new StringBuffer();  
        long starttime = System.currentTimeMillis();  
        for (String string : mlist) {  
            sb.append(string);  
        }  
        long endtime = System.currentTimeMillis();  
        System.out.println( "运行耗时:"+(endtime - starttime)+"ms"+"  多线程:执行"+COUNT+"次 StringBuffer.appeng() 操作 "); 
        return (endtime - starttime);
    }  
  
    /** 
     * 测试StringBuilder迭代赋值结果 
     *  
     * @param mlist 
     */  
    public static long doStringBuilderListTest(List mlist) {  
        StringBuilder sb = new StringBuilder();  
        long starttime = System.currentTimeMillis();  
        for (Iterator iterator = mlist.iterator(); iterator.hasNext();) {  
            sb.append(iterator.next());  
        }  
  
        long endtime = System.currentTimeMillis();  
        System.out.println( "运行耗时:"+(endtime - starttime)+"ms"+"  多线程:执行"+COUNT+"次 StringBuilder.appeng() 操作 "); 
        return (endtime - starttime);
    }  
  
    public static void main(String[] args) {  
         
        long BF = doStringBufferTest(); 
        long BD = doStringBuilderTest();
      //  long s = doStringTest(); 
       // Double h = Double.parseDouble(String.valueOf(s));
        Double i = Double.parseDouble(String.valueOf(BF));
        Double j = Double.parseDouble(String.valueOf(BD));
        
        System.out.println();
      //  System.out.println("运行耗时比    "+"String : StringBuffer = "+(h/i)+" : 1");
        System.out.println("运行耗时比    "+"StringBuilder : StringBuffer = "+(j/i)+" : 1");
    	System.out.println();
    
        List list = new ArrayList();
        for(long n=0;n


运行结果:

String,StringBuffer和StringBuilder之间的区别,速度比_第1张图片


你可能感兴趣的:(java)