[改善Java代码]自由选择字符串拼接方法
對一個字符串拼接有三種方法:加號,contact方法,StringBuffer或者StringBuilder的append方法,其中加號是最常用的.其他兩種方式偶爾會出現(xiàn)在一些開源項目中,那么這三者有什么區(qū)別?
//加號拼接str += "c";//concat方法連接str = str.concat("c");以上是兩種不同的字符串拼接方式,循環(huán)5萬次后再檢查執(zhí)行的時間,加號方式執(zhí)行的時間是1438毫秒,而concat方法的執(zhí)行時間是703毫秒,時間相差一倍,如果使用StringBuilder方式,執(zhí)行時間會更少.
1 public class Client { 2 public static final int MAX_LOOP = 50000; 3 public static void main(String[] args) { 4 doWithPlus(); 5 doWithConcat(); 6 doWithStringBuffer(); 7 8 String str ="abc"; 9 String str1 = str.concat("1"); 10 String str2 = "abc1"; 11 System.out.println(str1 == str2); 12 } 13 14 public static void doWithPlus(){ 15 String str = "a"; 16 long start = System.currentTimeMillis(); 17 for(int i=0;i<MAX_LOOP;i++){ 18 str += "c"; 19 //str = new StringBuilder(prefix).append("c").toString(); 20 } 21 long finish = System.currentTimeMillis(); 22 System.out.println("doWithPlus:" + (finish - start) + "ms"); 23 } 24 25 public static void doWithConcat(){ 26 String str = "a"; 27 long start = System.currentTimeMillis(); 28 for(int i=0;i<MAX_LOOP;i++){ 29 str = str.concat("c"); 30 } 31 long finish = System.currentTimeMillis(); 32 System.out.println("doWithConcat:" + (finish - start) + "ms"); 33 } 34 35 public static void doWithStringBuffer(){ 36 StringBuilder sb = new StringBuilder("a"); 37 long start = System.currentTimeMillis(); 38 for(int i=0;i<MAX_LOOP;i++){ 39 sb.append("c"); 40 } 41 String str = sb.toString(); 42 long finish = System.currentTimeMillis(); 43 System.out.println("doWithStringBuffer:" + (finish - start) + "ms"); 44 } 45 }?
運行結(jié)果:
doWithPlus:1559ms doWithConcat:748ms doWithStringBuffer:2ms false?
StringBuffer的append方法的執(zhí)行時間是0毫秒.說明時間非常的短(毫秒不足以計時,可以使用納秒進行計算).這個實驗說明在字符串拼接的方式中,append方法最快,concat方法次之,加號最慢,這是為何呢?
(1)"+"方法拼接字符串
雖然編譯器對字符串的加號做了優(yōu)化,它會使用StringBuilder的append方法進行追加,按道理來說,其執(zhí)行時間應(yīng)該也是0毫秒,不過它最終是通過toString方法轉(zhuǎn)換成String字符串的,例子中"+"拼接的代碼與如下代碼相同:
str = new StringBuilder(str).append("c").toString();?
它與純粹的使用StrignBuilder的append方法是不同的,意思每次循環(huán)都會創(chuàng)建一個StringBuilder對象,二是每次執(zhí)行完畢都要調(diào)用toString方法將其轉(zhuǎn)換為字符串------它的時間都耗費在這里了.
(2)concat方法拼接字符串
1 public String concat(String str) { 2 int otherLen = str.length(); 3 //如果追加的字符串長度為0,著返回字符串本身 4 if (otherLen == 0) { 5 return this; 6 } 7 int len = value.length; 8 char buf[] = Arrays.copyOf(value, len + otherLen); 9 //追加的字符串轉(zhuǎn)化成字符數(shù)組,添加到buf中 10 str.getChars(buf, len); 11 //復(fù)制字符數(shù)組,產(chǎn)生一個新的字符串 12 return new String(buf, true); 13 }?
其整體看上去就是一個數(shù)組的拷貝,雖然在內(nèi)存中的處理都是原子性操作,速度非常快,不過,注意看最后的return語句,每次的concat操作都會新創(chuàng)建一個String對象,這就是concat速度慢下來的真正原因,它創(chuàng)建了5萬個String對象.
(3)append方法拼接字符串
StringBuilder的append方法直接由父類AbstractStringBuilder實現(xiàn),其代碼如下....
public AbstractStringBuilder append(String str) {if (str == null) str = "null";//如果是null值,則把null作為字符串處理int len = str.length();ensureCapacityInternal(count + len);//加長,并作數(shù)組拷貝str.getChars(0, len, value, count);count += len;return this;}?
整個append方法都在做字符組處理,加長,然后數(shù)組拷貝,這些都是基本數(shù)據(jù)處理,沒有新建任何對象,所以速度也就最快了.
例子中是在最后通過StringBuffer的toString返回了一個字符串,也就是在5萬次循環(huán)結(jié)束之后才生成了一個String對象.
?
"+"非常符合我們的編碼習(xí)慣,適合人類閱讀,在大多數(shù)情況下都可以使用加號操作,只有在系統(tǒng)性能臨界的時候才考慮使用concat或者apped方法.
而且很多時候,系統(tǒng)的80%的系能消耗是在20%的代碼上,我們的精力應(yīng)該更多的投入到算法和結(jié)構(gòu)上.
?
轉(zhuǎn)載于:https://www.cnblogs.com/DreamDrive/p/5660256.html
總結(jié)
以上是生活随笔為你收集整理的[改善Java代码]自由选择字符串拼接方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: r$是什么货币
- 下一篇: 招商信用卡几个月可以提升额度
