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