JAVA properties and the string concatenation

Overview: This paper studies the performance of string concatenation of JAVA, the original text of the test code is not functionally equivalent, resulting in little test of significance concat. However, the original author in the comments column to a new concat result, if students are interested in their own proposals to modify the code test.

Splicing two strings in JAVA easiest way is to use the operator "+" a. If you are connecting to a fixed length string by "+", the performance may be slightly affected, but if you are in the cycle to "+" in a plurality of strings, then performance will decrease exponentially. Suppose there is a string, the string we will do a lot of cycling splicing operation, use the "+", then got the best performance. But exactly how this poor performance? If we also put StringBuffer, StringBuilder or String.concat () into the performance test, the result then? This article will give an answer to these questions!

We will use Per4j to calculate performance because this tool can give us a complete set of performance indicators, such as minimum, maximum, time-consuming, standard deviation and other statistical time period. In the test code in order to get an accurate standard deviation value, we will execute 20 mosaic "*" 50,000 tests. Here is the method we will use to splice the string:

Operator Concatenation (+)
String concat Method, - concat (String str)
StringBuffer Method, the append - the append (String str)
the StringBuilder Method, the append - the append (String str)
Finally, we will take a look at the byte code, how to study these methods in the end is to perform of. Now, let's begin to create my palpable class. Note that in order to calculate the performance of each cycle, each test code in the code need to be encapsulation Per4J library. First, let's define the number of iterations

1 private static final int OUTER_ITERATION=20;
2 private static final int INNER_ITERATION=50000;

Next, we will achieve our test code using the above four methods.
JAVA properties and the string concatenation

Next to generate performance metrics by running the program. I is a 64-bit operating environment Windown7 operating system, 32-bit JVM (7-ea) with 4GB of RAM, a dual-core CPU Quad 2.00GHz machine.

After 20 iterations, we get the following data:

JAVA properties and the string concatenation

The results perfect as we imagine. The only interesting thing is why the String.concat also very good, we all know, String is a regular class (the class does not change after initialization), then why concat performance will be better yet. (Translator's Note: In fact, the original author of the test code in question, to test code concat () method should be written concatTestStr = concatTestStr.concat ( "*") fishes.) To answer this question, we should look at concat decompiled out bytecode. In this paper download package which contains all the byte code, but now we look at concat of this code snippet:
JAVA properties and the string concatenation

This code is String.concat () bytecode from this code, we can clearly see, concat () method uses StringBuilder, concat () and StringBuilder performance should be as good, but due to the extra create StringBuilder and do .append (str) .append (str) .toString () operation, making concate performance will be affected to some extent, so StringBuilder and String Cancate time of 1.8 and 3.3.

Therefore, the instant when doing simple stitching, if we do not want to create the StringBuffer or StringBuilder instance, we also because of the use concat. However, for a large number of strings splicing operation, we should not use concat (Translator's Note: Since the test code is not fully functional equivalent, the average processing time of the test code concat 1650.9 milliseconds after the exchange is the result in the original comments inside.), because concat will reduce the performance of your program, you consume the cpu. Thus, without considering the thread safety and synchronization in order to obtain maximum performance, we should try to use StringBuilder

Guess you like

Origin blog.51cto.com/11395518/2476957