Last active
July 10, 2016 03:53
-
-
Save Dicee/ab8b8c47471999f721a6accde794dfc9 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
/** | |
* See http://stackoverflow.com/questions/38287661/how-to-get-tostring-for-each-element-of-an-arraylist/38287746?noredirect=1#comment63993889_38287746 | |
*/ | |
public static void main(String[] args) { | |
long start = System.currentTimeMillis(); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < 100000; i++) { | |
sb.append(i); | |
} | |
long timeWithStringBuilder = System.currentTimeMillis() - start; | |
start = System.currentTimeMillis(); | |
String s = ""; | |
for (int i = 0; i < 100000; i++) { | |
s = s + i; | |
} | |
long timeWithConcatenation = System.currentTimeMillis() - start; | |
System.out.println("With StringBuilder: " + timeWithStringBuilder + " ms\nWith concatenation: " + timeWithConcatenation + " ms"); | |
// Output: | |
// With StringBuilder: 67 ms | |
// With concatenation: 15665 ms | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment