码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA中,字符串STRING与STRINGBUILDER的效率差异

时间:2014-05-24 09:00:10      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

如果可变字符串操作较多的话,用STRINGBUILDER显然优势得多。

bubuko.com,布布扣
public class HelloJava {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "a";
        long starTime = System.currentTimeMillis();
        for(int i = 0; i<10000;i++){
            str = str + i;
        }
        long endTime = System.currentTimeMillis();
        long time = endTime - starTime;
        System.out.println("String time is " + time);
        System.out.println(str);
        StringBuilder builder = new StringBuilder("b");
        starTime = System.currentTimeMillis();
        for(int j = 0; j<10000;j++){
            builder = builder.append(j);
        }
        endTime = System.currentTimeMillis();
        time = endTime - starTime;
        System.out.println("StringBuilder time is " + time);
        System.out.println(builder);
        

    }

}
bubuko.com,布布扣

bubuko.com,布布扣

JAVA中,字符串STRING与STRINGBUILDER的效率差异,布布扣,bubuko.com

JAVA中,字符串STRING与STRINGBUILDER的效率差异

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/aguncn/p/3747194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!