Created
November 2, 2018 19:31
-
-
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
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 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