Java String Concatenation and Performance(String串联4个操作性能对比)

来源:互联网 发布:老千 电影 知乎 编辑:程序博客网 时间:2024/06/03 20:11

 


The quick and dirty way to concatenate strings in Java is touse the concatenation operator (+). This will yield a reasonableperformance if you need to combine two or three strings (fixed-size).But if you want to concatenate n strings in a loop, the performancedegrades in multiples of n. Given that String is immutable, for largenumber of string concatenation operations, using (+) will give us aworst performance. But how bad ? How StringBuffer, StringBuilder orString.concat() performs if we put them on a performance test ?. Thisarticle will try to answer those questions.

We will be using Perf4Jto calculate the performance, since this library will give usaggregated performance statistics like mean, minimum, maximum, standarddeviation over a set time span. In the code, we will concatenate astring (*) repeatedly 50,000 times and this iteration will be performed21 times so that we can get a good standard deviation. The followingmethods will be used to concatenate strings.

  • Concatenation Operator (+)
  • String concat method - concat(String str)
  • StringBuffer append method - append(String str)
  • StringBuilder append method - append(String str)

Andfinally we will look at the byte code to see how each of theseoperations perform. Let’s start building the class. Note that each ofthe block in the code should be wrapped around the Perf4J library tocalculate the performance in each iteration. Let’s define the outer andinner iterations first.


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

Now let’s implement each of the four methods mentioned in thearticle. Nothing fancy here, plain implementations of (+),String.concat(), StringBuffer.append() & StringBuilder.append().


  
 

Let’s run this program and generate the performance metrics. I ranthis program in a 64-bit OS (Windows 7), 32-bit JVM (7-ea), Core 2 QuadCPU (2.00 GHz) with 4 GB RAM.

The output from the 21 iterations of the program is plotted below.

 

Well, the results are pretty conclusive and as expected. Oneinteresting point to notice is how better String.concat performs. Weall know String is immutable, then how the performance of concat isbetter. To answer the question we should look at the byte code. I haveincluded the whole byte code in the download package, but let’s have alook at the below snippet.

  
 

This is the byte code for String.concat(), and its clear from thisthat the String.concat is using StringBuilder for concatenation and theperformance should be as good as String Builder. But given that thesource object being used is String, we do have some performance loss inString.concat.

So for the simple operations we should use String.concat compared to(+), if we don’t want to create a new instance of StringBuffer/Builder.But for huge operations, we shouldn’t be using the concat operator, asseen in the performance results it will bring down the application toits knees and spike up the CPU utilization. To have the bestperformance, the clear choice is StringBuilder as long as you do notneed thread-safety or synchronization.

The full source code, compiled class & the byte code is available for download in the below link.

Download Source, Class & Byte Code: String_Concatenation _Performance.zip

原创粉丝点击