"StringBuffer" and "String" quality competition
Read cherami wrote "String or StringBuffer?" Behind the number of netizens, as well as the comments, I feel this issue is necessary to properly investigate, prepared a simple test category and a script to run it. Laws passed that test parameters for different categories, and in different JDK testing discovered the problem really is a very interesting question. Let's get started.
First step in the preparatory work
In order to facilitate the work of the latter test, it is necessary to prepare a simple script:
According to the above script needs can be applied to windows or linux, I conducted tests in linux, so I kept it as a document stringtest.sh, if you windows on the test, you can save for stringtest.bat.
Note: In this paper, the results are running behind the continuous operation and many of them take a more typical sample and the average value)
The second step, began to write code
The first I almost did not consider how to write the following code:
The result:
Oh, is not a big accident? ! ! ! I also started, but Do not, in fact I made a mistake, as a result of the string + direct operation of the string are, in the compiler to compile time optimization, String s = "This is a" + "long test string for the" + "different JDK performance" + "testing."; compiled after a fact: String s = "This is a long test string for different JDK performance testing.";, Oh, this is a simple assignment, and no wonder the time spent almost no.
The third step is to amend the code
The result:
Looking at the results from the above we really can be "use String or StringBuffer?" In the conclusions, but also can see the different versions of the JDK performance difference is quite large, the performance of the worst JDK1.2.2 and JDK1. 3.1 the best performance.
The end of the discussion? Oh, Buyaoji, it is far from ending. :)
The fourth step is to reduce cycle times (from 10,000, a 1000)
The result:
What do you see? The above conclusion has been overthrown, the direct link to the operation of the string of StringBuffer than the use of the good performance, whether it is in that version of the JDK, but also in the performance JDK1.3.1 still is the highest. We can not but ask why. I regret that I do not know why. My idea is above the main difference between the two procedures is that the number of recycling, and the number of different led to the creation of the number of objects is the use of different memory, a different number of different operations is a result of JIT to the run-time optimization Strength different. These two factors I think that the only outcome of the proceedings, there will certainly be affected, but the impact is, I do not know.
To here, maybe we have the end of the story, but from the above results, I feel the need to do some tests.
Step 5, the intermediate results of all string together
The result:
We finally see the great advantages of the use of StringBuffer! And you may also be noted that a very interesting phenomenon: JDK1.3.1 the performance of a performance here seems not so conspicuous. StringBuffer even in the worst performance in the. Very interesting, is not it?
Step 6, and again reduce cycle times (from the 1000 reduction of 500)
The result:






