Created
February 22, 2016 18:30
-
-
Save addicted2sounds/04facc12997832f90687 to your computer and use it in GitHub Desktop.
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
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