Java string stitching and performance analysis detailed _java

Source: Internet
Author: User
Tags stringbuffer

Assuming there is a string, we will do a lot of looping on this string, and using "+" will get the lowest performance. But how bad is this performance? What happens if we put Stringbuffer,stringbuilder or String.Concat () in the performance test at the same time? This article will give an answer to these questions!

We will use PER4J to compute performance because the tool can give us a complete set of performance metrics, such as minimum, maximum time consuming, and poor standard for statistical periods. In the test code, in order to get an accurate standard deviation value, we will perform 20 stitching "*" 50,000 times of the test. Here's how we'll use the concatenation string:

Copy Code code as follows:

Concatenation Operator (+)
String concat method–concat (String str)
StringBuffer Append method–append (String str)
StringBuilder Append method–append (String str)

Finally, we'll look at bytecode to see how these methods actually work. Now, let's start by creating the class that I have palpable. Note In order to calculate the performance of each cycle, each test code in the code needs to be encapsulated with the PER4J library. Let's first define the number of iterations

Copy Code code as follows:

private static final int outer_iteration=20;
private static final int inner_iteration=50000;

Next, we will use the above 4 methods to implement our test code.

Copy Code code as follows:

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.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 ();

}

for (int outerindex=0;outerindex<=outer_iteration;outerindex++) {

Stopwatch stopwatch = new Loggingstopwatch ("Stringbuilderconcat");

CONCATTESTSBU = new StringBuilder ();

for (int innerindex=0;innerindex<=inner_iteration;innerindex++)

Concattestsbu.append ("*");

Stopwatch.stop ();

}


Next, run the program to generate the performance metrics. My operating environment is a 64-bit WINDOWN7 operating system, 32-bit JVM (7-EA) with 4GB memory, dual-core quad 2.00GHz CPU machine

The result was perfect, as we had imagined. The only interesting thing is why String.Concat is also good, and we all know that string is a regular class (a class that doesn't change after initialization), so why Concat's performance would be better. (Translator Note: In fact, the original author's test code is problematic, for the concat () method of test code should be written Concatteststr=concatteststr.concat ("*"). To answer this question, we should look at the concat bytecode. All the bytecode is included in the download package for this article, but now let's take a look at the code snippet for concat:

Copy Code code as follows:

46:new #6; Class Java/lang/stringbuilder
49:dup
50:invokespecial #7; Method Java/lang/stringbuilder. " <init> ":() V
53:aload_1
54:invokevirtual #8; Method Java/lang/stringbuilder.append:
(ljava/lang/string;) Ljava/lang/stringbuilder;
57:LDC #9; String *
59:invokevirtual #8; Method Java/lang/stringbuilder.append:
(ljava/lang/string;) Ljava/lang/stringbuilder;
62:invokevirtual #10; Method Java/lang/stringbuilder.tostring: ()
ljava/lang/string;
65:astore_1
66:iinc 7, 1
69:goto 38

This code is the byte Code of String.Concat (), from which we can see clearly that the concat () method uses Stringbuilder,concat () with the same performance as StringBuilder, However, due to the additional creation of StringBuilder and the operation of. Append (str). Append (str). toString (), concate performance can be affected, so StringBuilder and string The cancate time is 1.8 and 3.3.

So, immediately in doing the simplest stitching, if we don't want to create StringBuffer or StringBuilder instances, we also have to use concat. However, for a large number of string concatenation operations, we should not use Concat: Because the test code functionality is not entirely equivalent, the average processing time of the replacement test code concat is 1650.9 milliseconds. This result is in the original comment. Because Concat lowers your program's performance and consumes your CPU. Therefore, in order to achieve the highest performance without considering thread safety and synchronization, we should try to use StringBuilder.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.