Skip to content

Instantly share code, notes, and snippets.

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