Java String Concatenation and Performance(String串联4个操作性能对比)
来源:互联网 发布:老千 电影 知乎 编辑:程序博客网 时间:2024/06/03 20:11
Published by Venish Joe on Sunday, November 08, 2009
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.
1
private
static
final
int
OUTER_ITERATION=
20
;
2
private
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
- Java String Concatenation and Performance(String串联4个操作性能对比)
- Java String vs StringBuilder vs StringBuffer Concatenation Performance Micro Benchmark
- String Concatenation
- String concatenation
- String,StringBuilder性能对比
- javascript string 性能对比
- String.replace()和String.replaceAll()性能对比
- String/StringBuilder/StringBuffer性能对比
- Avoiding String Concatenation
- string concatenation demo
- Safe string concatenation
- The Smallest String Concatenation
- Delphi's String与Java String对比
- StringBuilder vs String concatenation in toString() in Java
- Performance String.+ vs String.Format
- string与StringBuilder的性能对比
- string与stringbuffer的性能对比
- String、StringBuffer和StringBuilder的性能对比
- javascript分页效果的实现
- 新浪名人博客ip流量来源分析
- tomcat 绑定域名
- 2009.11.10日再次回归,开启新的旅程
- tcp传输的流程
- Java String Concatenation and Performance(String串联4个操作性能对比)
- 内容页超连接关键词的完美实现
- 使用Hibernate.initialize(Object obj)方法初始化一个代理对象或者集合
- Telnet的命令模式
- 正则表达式之道
- MT6516智能芯片
- C# 后台动态生成textbox
- 我的CSDN
- 获取动态textbox的Text