0
0

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]文字列結合の実行速度比較(+演算子 vs StringBuilder)

Posted at

はじめに

"ほげ"を10万回繰り返して連結してみます。

+演算子

String result = "";
  for (var i = 0; i < 100000; i++) {
  result += "ほげ";
}

StringBuilder

var builder = new StringBuilder();
for (var i = 0; i < 100000; i++) {
  builder.append("ほげ");
}
var result = builder.toString();

実行結果

実行時間(ms)
+演算子 5709
StringBuilder 9

まとめ

圧倒的StringBuilderの勝利。
+演算子で連結する方法は、内部的にはStringオブジェクトの生成/破棄を繰り返し行なっている分、時間がかかってしまうようですね。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?