On the efficiency of StringBuilder class in C # and its comparison with string _c# tutorial

Source: Internet
Author: User
Tags volatile

In C #, when dealing with string concatenation, the efficiency of using StringBuilder is much higher than that of recklessly connected strings. Exactly how high, as follows:

static void Main (string[] args)
{
 string str1 = string. Empty;
 Stopwatch SW1 = new stopwatch ();
 SW1. Start ();
 for (int i = 0; I < 10000 i++)
 {
 str1 = str1 + i.tostring ();
 }
 SW1. Stop ();
 Console.WriteLine ("The time spent stitching strings is:" + SW1.) Elapsedmilliseconds + "millisecond");
 StringBuilder str2 = new StringBuilder (10000);
 Stopwatch SW2 = new stopwatch ();
 SW2. Start ();
 for (int i = 0; I < 10000 i++)
 {
 str2. Append (i.ToString ());
 }
 SW2. Stop ();
 Console.WriteLine ("The time spent using StringBuilder is:" + SW2.) Elapsedmilliseconds + "millisecond");
 Console.readkey ();
}

The above code performs the following effects:

The special thing about the string type is that we can use the string type just like a value type, whereas the string is actually a reference type. Since it is a reference type, the CLR saves the string type on the managed heap. When we use STR1 = str1 + i.tostring (); concatenation, because of the constant type of string, does not change the STR1 address in memory, but instead creates another string object on the managed heap. So, stitching 10,000 times, you create 10,000 string objects, the efficiency is inevitably low.

And StringBuilder will open a continuous memory in memory, when the increase in the string is actually for the same piece of memory modification, so more efficient.

Of course, the use of recklessly strings, or use of StringBuilder, is not absolute, depends on the situation. When stitching string is few, of course, directly recklessly to connect the string on the line.

Depth of string and StringBuilder differences
The String object is immutable. Each time you use one of the methods in the System.String class, or when you perform an operation (such as assignment, stitching, and so on), you create a new string object in memory, which requires allocating memory space for the new object, and StringBuilder does not. The system overhead associated with creating a new string object can be expensive when you need to perform a duplicate modification of the string. If you want to modify a string without creating a new object, you can use the System.Text.StringBuilder class. For example, when you concatenate many strings together in a loop, using the StringBuilder class can improve performance.

The characteristics of a String type object:
1. It is a reference type, allocating memory on the heap
2. A new instance will be generated when the operation is
3.String object Once generated cannot be changed (immutable)
4. Define equality operators (= = and!=) to compare the value of a String object (not a reference)

We all know that string objects are "immutable",
The method that operates on a string actually returns a new string object.
In the previous example, the two strings containing "orange" and "red" remained unchanged when the contents of S1 and S2 were concatenated to form a string. The + = operator creates a new string that contains the combined content. The result is that S1 now references a completely different string. A string containing only "orange" still exists, but is no longer referenced when the S1 is connected.
When a lot of strings are added, there will be a lot of things like S1 that are not referenced, resulting in a great waste of resources.
Everybody, look at this.

string stringvalue = This.m_stringvalue;

internal volatile string M_stringvalue;

Write here, need someone to see volatile, perhaps don't understand what meaning, probably said.
The volatile keyword implements data synchronization between threads, and a variable modified with volatile does not allow a copy of a variable different from the "main" memory area.
In other words, a variable that has been modified by volatile must be synchronized across all threads, and any thread that has changed its value, all other threads immediately
Gets the same value. Of course, the volatile-modified variable accesses more resources than a generic variable, because the thread has its own
Variable copies are more efficient.

This. Needsallocation (StringValue, Requiredlength)

Redistribute it only when it is needed.
In terms of allocation space and thread usage, StringBuilder is certainly higher than string, but only if it is used in a relatively high frequency.

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.