Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created January 18, 2013 20:23
Show Gist options
  • Save TheBatScripts/4568213 to your computer and use it in GitHub Desktop.
Save TheBatScripts/4568213 to your computer and use it in GitHub Desktop.
String vs StringBuilder
public class StringVsStringBuilder {
public static void main(String[] args) {
long start = System.nanoTime();
String a = "";
for(int i = 0; i < 10000; i++){
a += "a";
}
System.out.println("Time for a String to build: " + (System.nanoTime() - start) + "ns");
start = System.nanoTime();
StringBuilder b = new StringBuilder();
for(int i = 0; i < 10000; i++){
b.append("a");
}
System.out.println("Time for a StringBuilder to build: " + (System.nanoTime() - start) + "ns");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment