| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 849 人关注过本帖
标题:String,StringBuffer,StringBuilder
取消只看楼主 加入收藏
qingsiyu
Rank: 1
来 自:大连
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-10-6
收藏
 问题点数:0 回复次数: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 
2008-10-06 11:49
快速回复:String,StringBuffer,StringBuilder
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028787 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved