【就你不知道-Java 字符串】

来源:互联网 发布:澳大利亚商会 知乎 编辑:程序博客网 时间:2024/04/30 10:10


也行你知道,也许你不知道

一:

@Testpublic void test() {long start = System.currentTimeMillis();String str = "";for(int i = 0 ; i < 499990 ;i++){str = str+i;}long end = System.currentTimeMillis();System.out.println(start);System.out.println(end);System.out.println((double)(end-start)/1000);}

1335404638171
1335406659968
2021.797

二:

@Testpublic void test2() {long start = System.currentTimeMillis();StringBuffer str = new StringBuffer();for(int i = 0 ; i < 499999 ;i++){str.append(i);}long end = System.currentTimeMillis();System.out.println(start);System.out.println(end);System.out.println((double)(end-start)/1000);}

1335406936015
1335406936140
0.125


三:

@Testpublic void test2() {long start = System.currentTimeMillis();StringBuffer str = new StringBuffer();for(int i = 0 ; i < 1999999 ;i++){str.append(i);}long end = System.currentTimeMillis();System.out.println(start);System.out.println(end);System.out.println((float)(end-start)/1000);}

1335409426250
1335409426593
0.343

这下,你知道了吧


原创粉丝点击