Comparison between new capabilities of string and StringBuilder

Source: Internet
Author: User

The difference between string and StringBuilder is not described in detail here. After all, there are a lot of articles on the Internet. Here I will directly post a comparison of new capabilities for testing. You will know the differences between them at a glance.

First, let's briefly explain string and StringBuilder;

The string object cannot be changed. Every time you use a method in the System. String class, a New String object will be created in the memory. At this time, you need to allocate new space for the new object in the memory. When you duplicate a string (for example, String a = "1"; a + = 1;), the system overhead is very high compared with creating a new string object.

The StringBuilder object is a dynamic object with variable length. When you modify StringBuilder, it will first default a space. When the length reaches this capacity, it will automatically allocate new space and double the capacity, so it is an object that can be automatically modified.

If you don't talk nonsense, paste the relevant performance test code:

StringBuilder test code

[Csharp]
Protected void Button3_Click (object sender, EventArgs e)
{
Long startData = DateTime. Now. Ticks;
StringBuilder sb = new StringBuilder ();
Int count = int. Parse (TextBox2.Text );
For (int I = 0; I <count; I ++)
{
Sb. Append ("test log writing" + I + "," + TextBox1.Text + "\ r \ t ");
}
New WriteLog (). WriteLog_1 ("StringBuilder test write log", sb. ToString ());
Long endData = DateTime. Now. Ticks;
This. TextBox5.Text = TimeSpan. FromTicks (endData-startData). TotalMilliseconds + "";
}
StringBuilder test execution result

 

 

String test code

[Csharp]
Protected void Button4_Click (object sender, EventArgs e)
{
Long startData = DateTime. Now. Ticks;
String sb = "";
Int count = int. Parse (TextBox2.Text );
For (int I = 0; I <count; I ++)
{
Sb + = "test write log" + I + "," + TextBox1.Text + "\ r \ t ";
}
New WriteLog (). WriteLog_1 ("String test write log", sb );
Long endData = DateTime. Now. Ticks;
This. TextBox5.Text = TimeSpan. FromTicks (endData-startData). TotalMilliseconds + "";
}
String test execution result


Author: yang_5
 

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.