Skip to content

Instantly share code, notes, and snippets.

@komamitsu
Created January 29, 2013 07:15
Show Gist options
  • Select an option

  • Save komamitsu/4662412 to your computer and use it in GitHub Desktop.

Select an option

Save komamitsu/4662412 to your computer and use it in GitHub Desktop.
void bench() {
StringBuilder buf = new StringBuilder();
for (int i = 0; i<100000; i++)
buf.append("0123456789あいうえおaiueo");
String s = buf.toString();
byte[] bytes = s.getBytes();
char[] chars = s.toCharArray();
long start = 0;
start = System.currentTimeMillis();
String z = new String(chars);
p("srtring from string[" + z.length() + "]: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
int len = s.length();
int idx = 0;
StringBuilder sb = new StringBuilder();
while (idx < len) {
char c = s.charAt(idx++);
sb.append(c);
}
String y = sb.toString();
p("string from stringbuf[" + y.length() + "]: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
String x = new String(bytes, "UTF-8");
p("string from bytes[" + x.length() + "]: " + (System.currentTimeMillis() - start));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment