Skip to content

Instantly share code, notes, and snippets.

@Dicee
Last active July 10, 2016 03:53
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 Dicee/ab8b8c47471999f721a6accde794dfc9 to your computer and use it in GitHub Desktop.
Save Dicee/ab8b8c47471999f721a6accde794dfc9 to your computer and use it in GitHub Desktop.
/**
* 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