当前位置: 代码迷 >> Java Web开发 >> String,StringBuffer,StringBuilder
  详细解决方案

String,StringBuffer,StringBuilder

热度:573   发布时间:2008-10-06 11:49:59.0
String,StringBuffer,StringBuilder
用String,StringBuffer,StringBuilder测试一下速度,发现在Netbeans,Jcreator里执行结果有一定的差异
StringBuffer应该是线程安全的,带有Sychronized的,而StringBuilder是以牺牲线程安全为代价的,在单线程情况下
应该会比StringBuffer来得快,在Netbeans得到了想要的结果.而在Jcreator里却是StringBuffer来得快一些..
这是为什么???
下附代码和结果:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package comparestrings;
//import java.util.*;
/**
*
* @author lynn
*/
public class CompareStrings {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         String str = "abcdefghijklmnopqrstuvwxyz";
         StringBuffer strBuffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
         StringBuilder strBuilder = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
        long time1= System.currentTimeMillis() ;
        for(int i= 0 ;i<10000;i++)
            str = str+"abcdefghijklmnopqrstuvwxyz";
        long time2= System.currentTimeMillis() ;
        long t1 = time2 - time1;
        
        time1= System.currentTimeMillis() ;
        for(int i= 0 ;i<10000;i++)
            strBuffer.append("abcdefghijklmnopqrstuvwxyz");
        time2= System.currentTimeMillis() ;
        long t2 = time2 - time1;
        
        time1= System.currentTimeMillis() ;
        for(int i= 0 ;i<10000;i++)
            strBuilder.append("abcdefghijklmnopqrstuvwxyz");
        time2= System.currentTimeMillis() ;
        long t3 = time2 - time1;
         
        // System.out.println(str);
         System.out.println("use time: "+t1 +" \n\n**********************************");
         //System.out.println(strBuffer.toString());
         System.out.println("use time: "+t2 +" \n\n**********************************");
         //System.out.println(strBuilder.toString());
         System.out.println("use time: "+t3 );
    }

}
NetBeans运行结果:
run:
use time: 50953

**********************************
use time: 16

**********************************
use time: 0


Jcreator的运行结果:
搜索更多相关主题的帖子: String  

----------------解决方案--------------------------------------------------------
MyEclipse 测试结果
use time: 73610

**********************************
use time: 0

**********************************
use time: 0
----------------解决方案--------------------------------------------------------
额外测试
API:
例如,测试某些代码执行的时间长度:
   long startTime = System.nanoTime();
   // ... the code being measured ...
   long estimatedTime = System.nanoTime() - startTime;

结果:
use time: 75743151993

**********************************
use time: 7904077

**********************************
use time: 6222020
----------------解决方案--------------------------------------------------------
nb更专业点,jc本身bug就比较多。
直接命令行应该是最准的。

测这种东西没什么用,java不是c,这种速度上的优化对web工程影响不大。
最大的影响还是数据库读取方面的。
----------------解决方案--------------------------------------------------------
  相关解决方案