Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Created July 17, 2015 15:20
Show Gist options
  • Save TrevorS/cba05a0a9bb53376cb1c to your computer and use it in GitHub Desktop.
Save TrevorS/cba05a0a9bb53376cb1c to your computer and use it in GitHub Desktop.
import java.util.stream.IntStream;
public class TestSB {
private static String a = "";
private static String b = "";
private static int iterations = 100000;
private static long start;
public static void main(String[] args) {
start = System.nanoTime();
IntStream.range(0, iterations).forEach(i -> a += i);
System.out.println("concat took: " + (System.nanoTime() - start) + " ms.");
start = System.nanoTime();
StringBuilder sb = new StringBuilder();
IntStream.range(0, iterations).forEach(i -> sb.append(i));
String s = sb.toString();
System.out.println("sb took: " + (System.nanoTime() - start) + " ms.");
}
}
// concat took: 8298702665 ms.
// sb took: 7196031 ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment