Created
May 4, 2014 14:24
-
-
Save YujiSoftware/b2c48d3d83c549b09c6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String stringBuilderJoin2(){ | |
StringBuilder s = new StringBuilder("["); | |
for(int i = 0; i < strarray.length; ++i){ | |
if(i != 0){ | |
s.append(']').append(',').append('['); | |
} | |
s.append(strarray[i]); | |
} | |
s.append(']'); | |
return s.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
きしださんのブログに掲載されていた stringBuilderJoin のちょっとだけ高速化版。
Java8時代の文字列連結まとめ - きしだのはてな
http://d.hatena.ne.jp/nowokay/comment?date=20140409
delimiter や prefix を String から char に変えただけですが、速くなりました。
stringBuilderJoin:1807ms
stringBuilderJoin2:1482ms
これは、StringBuilder の append(String) では null チェックや長さの取得、文字の分解などを行っているのに対して、append(char) は文字を配列の末尾に追加するだけだからと考えられます。
ちなみに、new StringBuilder('[') としていないのは、引数に char をとるコンストラクタがないためです。