String and StringBuilder (1), string String operations under the string connection operation

Source: Internet
Author: User

String and StringBuilder (1), string String operations under the string connection operation

See the first scenario: Use a String to connect to the database for 10 thousand times.

Public class Demo_01 {public static void main (String [] args) {String str = "123"; String [] temp = new String [10000]; for (int I = 0; I <temp. length; I ++) {temp [I] = "456";} long begin = System. nanoTime ()/000000l; System. out. println (begin); for (int I = 0; I <temp. length; I ++) {str + = temp [I];} long end = System. nanoTime ()/000000l; System. out. println (end); System. out. println ("connection operation time consumed:" + (end-begin) + "millisecond ");}}

Compile and execute

 

 

The same string connection is also connected 10 thousand times this time using StringBuilder

Public class Demo {public static void main (String [] args) {StringBuilder sb = new StringBuilder (); sb. append ("123"); String [] temp = new String [10000]; for (int I = 0; I <temp. length; I ++) {temp [I] = "456";} long begin = System. nanoTime ()/000000l; System. out. println (begin); for (int I = 0; I <temp. length; I ++) {sb. append (temp [I]);} long end = System. nanoTime ()/000000l; System. out. println (end); System. out. println ("connection operation time consumed:" + (end-begin) + "millisecond ");}}

Compile and execute

 

The time consumed by the comparison execution. String is 479 Ms. StringBuilder is 2 ms. (The number of milliseconds is for reference only. Different Pc configurations and OS environment factors may vary)

When using StringBuilder for the same String connection operation, you only need to use about 1/240 of the String.

It can be seen that StringBuilder is more efficient when a large number of string connections are made.

Anyone who has learned Java knows this. You can use it.

 

But why is StringBuilder more efficient when a large number of string connections are made?

This is a question worth thinking about.

 

Well, let's take a look at the feedback from the compiler.

For String-based connection, use the disassembly command

Obtain the following information:

We can see that 79 rows of goto statements point to 47 rows, which is a loop body.

Hmm? I use a String, but in fact the compiler is good at making claims using StringBuilder for connection operations. (StringBuilder is highly efficient)

Note that 54 rows will create a new StringBuilder object each time.

 

Let's look at the operations using StringBuilder:

Feedback

Note that 78 rows of goto statements point to 59 rows.

However, this loop body only has one StringBuilder object. The compiler does not create a new StringBuilder object every cycle.

Therefore, StringBuilder is more efficient in connection with a large number of strings than String.

 

The next section will discuss the reasons why StringBuilder is more efficient from the source code perspective.


String and StringBuilder in c #

The string class is non-mutable (that is, when making any changes to a string object, it is actually creating another string class object ), therefore, when you need to change a string object frequently, we recommend that you use the StringBuilder class. The principle of the StringBuilder class is to first open up a certain amount of memory space in the memory, when this StringBuilder class object is changed, if the memory space is not enough, the memory space will be expanded, instead of re-creating an object, so that if you perform frequent operations on a string object, it will not cause too much memory waste. In fact, there is no major difference in nature. It is used to store and operate strings. The only difference lies in the performance. Hope to help you.

What is the difference between the String class and the Stringbuilder class?

This is the case for comparison. String determines whether to create a new instance based on the content.

In addition, the two functions are different. StringBuilder is more like List

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.