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:
Test Code:
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 version | StringBuilder To Array | StringBuilder CharAt | Array to array | Reflection to array |
Java HotSpot (TM) Client VM (build 1.5.0_14-b03, mixed mode) | 1667 | 1914 | 923 | 692 |
Java HotSpot (TM) Server VM (build 1.5.0_14-b03, mixed mode) | 1805 | 1374 | 1064 | 717 |
Java HotSpot (TM) Client VM (build 10.0-b23, mixed mode, sharing) | 1458 | 1309 | 756 | 517 |
Test Code:
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));
- }