Created
January 29, 2013 07:15
-
-
Save komamitsu/4662412 to your computer and use it in GitHub Desktop.
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
| 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