Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Last active August 29, 2015 14:08
Show Gist options
  • Save TrevorS/78afcbad268a0a8bf8f7 to your computer and use it in GitHub Desktop.
Save TrevorS/78afcbad268a0a8bf8f7 to your computer and use it in GitHub Desktop.
"" + c vs. Character.toString(c)
public class TestChar {
public static void main(String[] args) {
long tests = 1000000000;
char c = 'c';
long start = System.nanoTime();
for (int i = 0; i < tests; i++) {
String a = "" + c;
}
System.out.println("Done: " + (System.nanoTime() - start) / 1000000000.0);
start = System.nanoTime();
for (int i = 0; i < tests; i++) {
String a = Character.toString(c);
}
System.out.println("Done: " + (System.nanoTime() - start) / 1000000000.0);
}
}
/**
* ➜ ~ java TestChar
* Done: 3.36814261
* Done: 3.232308615
* ➜ ~
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment