The difference between string and StringBuilder

Source: Internet
Author: User

1, analysis of the difference between string and StringBuilder, we first look at the following section of code

First we use string for concatenation of strings

Class program    {        static void Main (string[] args)        {            //stringbuilder sb = new StringBuilder ();            string str = null;            Create a timer that calculates the time that the program runs            Stopwatch SW = new Stopwatch ();            Start Timing            SW. Start ();            for (int i = 1; i < 10000;i++)            {                //sb. Append (i);                str + = i;            }            End Timing            SW. Stop ();            Console.WriteLine (sb.) ToString ());            Console.WriteLine (str);            The output program runs at time            Console.WriteLine (SW. Elapsed);            Console.readkey ();        }    }

The results of the program run as follows:

We can see that at this point the run time of the string program is about 0.125 seconds.

Now we'll use Stringbulider.

Class program    {        static void Main (string[] args)        {            StringBuilder sb = new StringBuilder ();            string str = null;            Create a timer that calculates the time that the program runs            Stopwatch SW = new Stopwatch ();            Start Timing            SW. Start ();            for (int i = 1; i < 10000;i++)            {                sb. Append (i);                str + = i;            }            End Timing            SW. Stop ();            Console.WriteLine (sb.) ToString ());            Console.WriteLine (str);            Output program run time            Console.WriteLine (SW. Elapsed);            Console.readkey ();        }    }

At this point the program runs with the result

We can see that using the StringBuilder program runs for only about 0.0021 seconds, compared with a string program that runs many times faster

2, then why StringBuilder run faster than a string so much?

First, we look at string:

(1), string string, is a reference type and has no variability.

We generally believe that the memory is divided into five regions. There are three commonly used programmers, namely stack, heap, static storage area.

Where the value of a value type is stored in the stack, such as the int type, and the value of the reference type is stored in the heap

As shown

And once we change the value of str str= "John Doe" looks like we changed the value of STR, actually

When str= "John Doe", we created a new object in the heap "John Doe" whose address naturally changed 0x001001 was killed, replaced by John Doe in the heap address 0x002003

It appears that we have changed the value of STR, but we have actually created a new object.

We can use a simple code to see how it actually works:

We use the monitor to track str where *STR traces its address and can see that the address of STR at this time is 0x00000000

Debugging with step-by-step statements

When the value of STR is "Zhang San", its address has changed

And when str= "John Doe", the address changed.

Therefore, the string type is immutable, and once its value has changed, it is a new object. That is, each time a string is manipulated, a new object is created.

(2), the StringBuilder class solves the problem of creating a large number of objects during repeated modifications to the string. The StringBuilder class allocates space to a string in char, so no additional memory allocation is required in the operation.

For StringBuilder we can also use the above code to illustrate.

We can see that the whole process does not change the address.

In summary, if you manipulate a small amount of data, we can use string

If there is a lot of character stitching, we should use StringBuilder.

Lofty high-rise ground, the foundation is the most important!

Some common methods of string and StringBuilder are summarized in the next blog post.

The difference between string and StringBuilder

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.