Created
January 18, 2013 20:23
-
-
Save TheBatScripts/4568213 to your computer and use it in GitHub Desktop.
String vs StringBuilder
This file contains hidden or 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
| 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