0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Java]StringBuilder、StringBuffer、プラス演算子、concat、joinによる文字列結合の処理速度比較

Posted at

文字列を結合する方法は色々あるけど「StringBuilder」か「StringBuffer」が推奨されているみたいなので、「StringBuilder」と「StringBuffer」とそれ以外でどのくらい処理速度が違うのか比較してみた。
比較したのは「StringBuilder」「StringBuffer」「プラス演算子」「concat」「join」で、"1"から"10000"までを文字列結合したときの処理速度を比較してみた。

結果から見てみると、

StringBuilder StringBuffer プラス演算子 concat join
2ms 3ms 208ms 102ms 8ms

となり、ほぼ予想通りでStringBuilderとStringBufferがダントツで早くプラス演算子とconcatが遅かったが、Listに詰める処理とか入ってるjoinが思いのほか早かった。
また、測定した各結合処理のコードは以下の通り。

StringBuilder.java
public static void main(String[] args) {
	String st = "";
	long startTime = System.currentTimeMillis();
	StringBuilder sbi = new StringBuilder();
	for(int i = 0; i < 10000; i++) {
		sbi.append(String.valueOf(i+1));
	}
	st = sbi.toString();
	long endTime = System.currentTimeMillis();
	System.out.println("文字列長さ:" +  st.length());
	System.out.println("処理時間:" +  (endTime - startTime) + "ms");
}
StringBuffer.java
public static void main(String[] args) {
	String st = "";
	long startTime = System.currentTimeMillis();
	StringBuffer sbf = new StringBuffer();
	for(int i = 0; i < 10000; i++) {
		sbf.append(String.valueOf(i+1));
	}
	st = sbf.toString();
	long endTime = System.currentTimeMillis();
	System.out.println("文字列長さ:" +  st.length());
	System.out.println("処理時間:" +  (endTime - startTime) + "ms");
}
StringPlus.java
public static void main(String[] args) {
	String st = "";
	long startTime = System.currentTimeMillis();
	for(int i = 0; i < 10000; i++) {
		st = st + String.valueOf(i+1);
	}
	long endTime = System.currentTimeMillis();
	System.out.println("文字列長さ:" +  st.length());
	System.out.println("処理時間:" +  (endTime - startTime) + "ms");
}
StringConcat.java
public static void main(String[] args) {
	String st = "";
	long startTime = System.currentTimeMillis();
	for(int i = 0; i < 10000; i++) {
		st = st.concat(String.valueOf(i+1)) ;
	}
	long endTime = System.currentTimeMillis();
	System.out.println("文字列長さ:" +  st.length());
	System.out.println("処理時間:" +  (endTime - startTime) + "ms");
}
StringJoin.java
public static void main(String[] args) {
	String st = "";
	long startTime = System.currentTimeMillis();
	List<String> list = new ArrayList<String>();
	for(int i = 0; i < 10000; i++) {
		list.add(String.valueOf(i+1));
	}
	st = String.join("", list);
	long endTime = System.currentTimeMillis();
	System.out.println("文字列長さ:" +  st.length());
	System.out.println("処理時間:" +  (endTime - startTime) + "ms");
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?