Created
August 25, 2014 16:53
-
-
Save ctrueden/f3869c48c6d8b0b73ea7 to your computer and use it in GitHub Desktop.
Repeated string concatenation is _much_ more expensive in both time and space
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
Randomizing stuff... | |
Concatenating... | |
9 [fast] | |
956 [slow] |
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
Randomizing stuff... | |
Concatenating... | |
19 [fast] | |
4841 [slow] |
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
Randomizing stuff... | |
Concatenating... | |
51 [fast] | |
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space | |
at java.util.Arrays.copyOf(Arrays.java:2882) | |
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100) | |
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390) | |
at java.lang.StringBuilder.append(StringBuilder.java:119) | |
at StringConcat.goSlow(StringConcat.java:10) | |
at StringConcat.main(StringConcat.java:41) |
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
import java.util.Random; | |
public class StringConcat { | |
public static String str; | |
public static void goSlow(String[] array) { | |
final long start = System.currentTimeMillis(); | |
String s = ""; | |
for (int i=0; i<array.length; i++) { | |
s += array[i]; | |
} | |
str = s; | |
final long end = System.currentTimeMillis(); | |
System.out.println(end - start + " [slow]"); | |
} | |
public static void goFast(String[] array) { | |
final long start = System.currentTimeMillis(); | |
final StringBuilder sb = new StringBuilder(); | |
for (int i=0; i<array.length; i++) { | |
sb.append(array[i]); | |
} | |
str = sb.toString(); | |
final long end = System.currentTimeMillis(); | |
System.out.println(end - start + " [fast]"); | |
} | |
public static void main(final String[] args) throws Exception { | |
System.out.println("Randomizing stuff..."); | |
final String[] array = new String[1024]; | |
final Random r = new Random(); | |
for (int i=0; i<array.length; i++) { | |
final byte[] bytes = new byte[10384]; | |
for (int b=0; b<bytes.length; b++) { | |
bytes[b] = (byte) r.nextInt(256); | |
} | |
array[i] = new String(bytes); | |
} | |
System.out.println("Concatenating..."); | |
goFast(array); | |
goSlow(array); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment