Skip to content

Instantly share code, notes, and snippets.

@addicted2sounds
Created February 22, 2016 18:30
Show Gist options
  • Save addicted2sounds/04facc12997832f90687 to your computer and use it in GitHub Desktop.
Save addicted2sounds/04facc12997832f90687 to your computer and use it in GitHub Desktop.
package com.company;
public class Main {
private static int iterations = 100000;
private static void testString() {
String str = "";
for (int i = 0; i < Main.iterations; i++) {
str += "x";
}
}
private static void testStringBuffer() {
StringBuffer str = new StringBuffer(Main.iterations);
for (int i = 0; i < Main.iterations; i++) {
str.append("x");
}
}
private static void testStringBuilder() {
StringBuffer str = new StringBuffer(Main.iterations);
for (int i = 0; i < Main.iterations; i++) {
str.append("x");
}
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
testString();
System.out.println("String operations took " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
testStringBuilder();
System.out.println("StringBuilder operations took " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
testStringBuffer();
System.out.println("StringBuffer operations took " + (System.currentTimeMillis() - time));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment