About expanding strings in Java

    Having read habrotopik About line expansion in .Net / C # and not only , I was interested and how things are with the same problem in Java.
    Without a slow memory machine, I had to confine myself to tests on one.
    There is no time to conduct as many tests as the author did in the original, therefore I will limit myself to checking a few thoughts, however, the trend in Java is observed - StringBuilder is the slowest result.



    Machine Parameters: Intel® Core (TM) 2 Duo CPU E4600 @ 2.40GHz; 4GB Ram DDR2 (unfortunately I won’t say the frequency) The
    swap is completely disconnected OS - WinXP SP2.

    Results in parrots:

    Java versionStringBuilder To ArrayStringBuilder CharAtArray to arrayReflection to array
    Java HotSpot (TM) Client VM (build 1.5.0_14-b03, mixed mode)16671914923692
    Java HotSpot (TM) Server VM (build 1.5.0_14-b03, mixed mode)180513741064717
    Java HotSpot (TM) Client VM (build 10.0-b23, mixed mode, sharing)14581309756517


    Test Code:

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

    Also popular now: