Java String Concatenation and Performance(String串联4个操作性能对比)
2009-11-10 11:10
441 查看
Published by Venish Joe on Sunday, November 08, 2009The quick and dirty way to concatenate strings in Java is to
use the concatenation operator (+). This will yield a reasonable
performance if you need to combine two or three strings (fixed-size).
But if you want to concatenate n strings in a loop, the performance
degrades in multiples of n. Given that String is immutable, for large
number of string concatenation operations, using (+) will give us a
worst performance. But how bad ? How StringBuffer, StringBuilder or
String.concat() performs if we put them on a performance test ?. This
article will try to answer those questions.We will be using Perf4J
to calculate the performance, since this library will give us
aggregated performance statistics like mean, minimum, maximum, standard
deviation over a set time span. In the code, we will concatenate a
string (*) repeatedly 50,000 times and this iteration will be performed
21 times so that we can get a good standard deviation. The following
methods 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)And
finally we will look at the byte code to see how each of these
operations perform. Let’s start building the class. Note that each of
the block in the code should be wrapped around the Perf4J library to
calculate the performance in each iteration. Let’s define the outer and
inner iterations first.
Now let’s implement each of the four methods mentioned in the
article. Nothing fancy here, plain implementations of (+),
String.concat(), StringBuffer.append() & StringBuilder.append().
Let’s run this program and generate the performance metrics. I ran
this program in a 64-bit OS (Windows 7), 32-bit JVM (7-ea), Core 2 Quad
CPU (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. One
interesting point to notice is how better String.concat performs. We
all know String is immutable, then how the performance of concat is
better. To answer the question we should look at the byte code. I have
included the whole byte code in the download package, but let’s have a
look at the below snippet.
This is the byte code for String.concat(), and its clear from this
that the String.concat is using StringBuilder for concatenation and the
performance should be as good as String Builder. But given that the
source object being used is String, we do have some performance loss in
String.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, as
seen in the performance results it will bring down the application to
its knees and spike up the CPU utilization. To have the best
performance, the clear choice is StringBuilder as long as you do not
need 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.zipPosted by
Venish Joe转自http://www.venishjoe.net
use the concatenation operator (+). This will yield a reasonable
performance if you need to combine two or three strings (fixed-size).
But if you want to concatenate n strings in a loop, the performance
degrades in multiples of n. Given that String is immutable, for large
number of string concatenation operations, using (+) will give us a
worst performance. But how bad ? How StringBuffer, StringBuilder or
String.concat() performs if we put them on a performance test ?. This
article will try to answer those questions.We will be using Perf4J
to calculate the performance, since this library will give us
aggregated performance statistics like mean, minimum, maximum, standard
deviation over a set time span. In the code, we will concatenate a
string (*) repeatedly 50,000 times and this iteration will be performed
21 times so that we can get a good standard deviation. The following
methods 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)And
finally we will look at the byte code to see how each of these
operations perform. Let’s start building the class. Note that each of
the block in the code should be wrapped around the Perf4J library to
calculate the performance in each iteration. Let’s define the outer and
inner iterations first.
1 | private static final int OUTER_ITERATION= 20 ; |
2 | private static final int INNER_ITERATION= 50000 ; |
article. Nothing fancy here, plain implementations of (+),
String.concat(), StringBuffer.append() & StringBuilder.append().
String addTestStr = ""; String concatTestStr = ""; StringBuffer concatTestSb = null; StringBuilder concatTestSbu = null; for (int outerIndex=0;outerIndex<=OUTER_ITERATION;outerIndex++) { StopWatch stopWatch = new LoggingStopWatch("StringAddConcat"); addTestStr = ""; for (int innerIndex=0;innerIndex<=INNER_ITERATION;innerIndex++) addTestStr += "*"; stopWatch.stop(); } for (int outerIndex=0;outerIndex<=OUTER_ITERATION;outerIndex++) { StopWatch stopWatch = new LoggingStopWatch("StringConcat"); concatTestStr = ""; for (int innerIndex=0;innerIndex<=INNER_ITERATION;innerIndex++) concatTestStr = concatTestStr.concat("*"); stopWatch.stop(); } for (int outerIndex=0;outerIndex<=OUTER_ITERATION;outerIndex++) { StopWatch stopWatch = new LoggingStopWatch("StringBufferConcat"); concatTestSb = new StringBuffer(); for (int innerIndex=0;innerIndex<=INNER_ITERATION;innerIndex++) concatTestSb.append("*"); stopWatch.stop(); }
this program in a 64-bit OS (Windows 7), 32-bit JVM (7-ea), Core 2 Quad
CPU (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. One
interesting point to notice is how better String.concat performs. We
all know String is immutable, then how the performance of concat is
better. To answer the question we should look at the byte code. I have
included the whole byte code in the download package, but let’s have a
look at the below snippet.
new #7; //class java/lang/StringBuilder dup invokespecial #8; //Method java/lang/StringBuilder."<init>":()V aload_1 invokevirtual #9; //Method java/lang/StringBuilder.append: (Ljava/lang/String;)Ljava/lang/StringBuilder; ldc #10; //String * invokevirtual #9; //Method java/lang/StringBuilder.append: (Ljava/lang/String;)Ljava/lang/StringBuilder; invokevirtual #11; //Method java/lang/StringBuilder.toString:() Ljava/lang/String; astore_1
that the String.concat is using StringBuilder for concatenation and the
performance should be as good as String Builder. But given that the
source object being used is String, we do have some performance loss in
String.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, as
seen in the performance results it will bring down the application to
its knees and spike up the CPU utilization. To have the best
performance, the clear choice is StringBuilder as long as you do not
need 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.zipPosted by
Venish Joe转自http://www.venishjoe.net
相关文章推荐
- Effective Java 51 Beware the performance of string concatenation
- Java String vs StringBuilder vs StringBuffer Concatenation Performance Micro Benchmark
- StringBuilder vs String concatenation in toString() in Java
- C++,JAVA,Go对比MySql数据库操作性能
- 关于java.lang.String的replace方法与apache-commons-lang的replace方法性能对比
- 2017年的golang、python、php、c++、c、java、Nodejs性能对比(golang python php c++ java Nodejs Performance)
- "String Concatenation vs String Builder - The performance hit! See it to believe it :o)"
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】
- 【转载】Java中将InputStream读取为String, 各种方法的性能对比
- java-IO操作性能对比
- java-IO操作性能对比
- Java中字符串操作类String、StringBuffer、StringBuilder区别及性能对比测试
- java String、String.concat和StringBuilder性能对比
- java-IO操作性能对比
- https://sqlwhisper.wordpress.com/2013/03/24/stuff-and-for-xml-path-for-string-concatenation/
- java-IO操作性能对比
- 【java performance】对于常量字符串,用'String' 代替'StringBuffer'
- Java几款性能分析工具的对比
- Memcached Java Client性能对比
- 基于Java Client的Redis与Tarantool HASH性能对比