Skip to content

Instantly share code, notes, and snippets.

@Delf-Lee
Created November 2, 2018 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Delf-Lee/d38d05075d3c198f521dd4b2d126cd50 to your computer and use it in GitHub Desktop.
Save Delf-Lee/d38d05075d3c198f521dd4b2d126cd50 to your computer and use it in GitHub Desktop.
The difference in concat speed between '+' operator of String and append() of StringBuilder
public class StringVsSringBuilder {
public static void main(String[] args) {
String string = "";
StringBuilder stringBuilder = new StringBuilder();
String value = "abcd";
/* String의 '+' 연산 */
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
string += value;
}
long end = System.currentTimeMillis();
System.out.println((end - start) / 1000.0 + "초");
/* StringBuilder의 append 연산 */
start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
stringBuilder.append(value);
}
end = System.currentTimeMillis();
System.out.println((end - start) / 1000.0 + "초");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment