Javaでの文字列の展開について

habrotopikを読んだ後、.Net / C#の行の拡張についてだけでなく 、Javaでも同じ問題がどのように発生するかについて興味がありました。
低速のメモリマシンがなければ、テストを1つに限定する必要がありました。
著者がオリジナルで行ったほど多くのテストを実行する時間がないため、いくつかの考えをチェックすることに自分自身を制限しますが、Javaの傾向が観察されます-StringBuilderは最も遅い結果です。



マシンパラメーター:Intel®Core(TM)2 Duo CPU E4600 @ 2.40GHz; 4GB Ram DDR2(残念ながら周波数は言いません)
完全に無効化されたOSのスワップ-WinXP SP2。

オウムの結果:

JavaバージョンStringBuilderから配列StringBuilder CharAt配列から配列アレイへの反射
Java HotSpot(TM)Client VM(ビルド1.5.0_14-b03、混合モード)16671914923692
Java HotSpot(TM)Server VM(ビルド1.5.0_14-b03、混合モード)180513741064717
Java HotSpot(TM)Client VM(ビルド10.0-b23、混合モード、共有)14581309756517


テストコード:

Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  1. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  2. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  3. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  4. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  5. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  6. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  7. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  8. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  9. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  10. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  11. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  12. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  13. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  14. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  15. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  16. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  17. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  18. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  19. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  20. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  21. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  22. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  23. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  24. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  25. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  26. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  27. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  28. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  29. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  30. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  31. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  32. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  33. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  34. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  35. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  36. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  37. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  38. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  39. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  40. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  41. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  42. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  43. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  44. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  45. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  46. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  47. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  48. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  49. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  50. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  51. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  52. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  53. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  54. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  55. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  56. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  57. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  58. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  59. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  60. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  61. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  62. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  63. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  64. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  65. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  66. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  67. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  68. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  69. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  70. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  71. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  72. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  73. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  74. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  75. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  76. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  77. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  78. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  79. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  80. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  81. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  82. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  83. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  84. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  85. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  86. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  87. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  88. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  89. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  90. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  91. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  92. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  93. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  94. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  95. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  96. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  97. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  98. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  99. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  100. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }
  101. Copy Source | Copy HTML private String testString = "" ; protected void setUp() throws Exception { int charCount = 50 * 1024 * 1024 ; Random rnd = new Random (); byte chars[] = new byte [charCount * 2 ]; rnd.nextBytes(chars); testString = new String (chars); } public long _testStringBuilderToArray() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(data[i]); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testStringBuilderCharAt() { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder (testString.length()); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { builder.append(testString.charAt(i)); } String reverse = builder.toString(); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testArrayToArray() { long start = System.currentTimeMillis(); char reverseBytes[] = new char [testString.length()]; char data[] = testString.toCharArray(); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public long _testReflectionToArray() throws Exception { long start = System.currentTimeMillis(); Field stringClassValueField = String . class .getDeclaredField( "value" ); stringClassValueField.setAccessible( true ); char reverseBytes[] = new char [testString.length()]; char data[] = ( char [])stringClassValueField. get (testString); int len = testString.length(); for ( int i = len - 1 ; i >= 0 ; i--) { reverseBytes[len - i - 1 ] = data[i]; } String reverse = new String (reverseBytes); if ((reverse.charAt( 2 ) == 'd' ) && (len == 1 )) { //To avoid compiler optimization return 0 ; } long end = System.currentTimeMillis(); return (end - start); } public void testRunner() throws Exception { long value = 0 ; int testCount = 20 ; //StringBuilderToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderToArray(); } System. out .println( "_testStringBuilderToArray - " + ( value / testCount)); //_testStringBuilderCharAt value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testStringBuilderCharAt(); } System. out .println( "_testStringBuilderCharAt - " + ( value / testCount)); //_testArrayToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testArrayToArray(); } System. out .println( "_testArrayToArray - " + ( value / testCount)); //_testReflectionToArray value = 0 ; for ( int i = 0 ; i < testCount; i++) { value += _testReflectionToArray(); } System. out .println( "_testReflectionToArray - " + ( value / testCount)); }

Source: https://habr.com/ru/post/J58382/


All Articles