The use of StringBuilder classes also plays an important role in ASP. NET skills. The StringBuilder class should be better at string processing.
The string is immutable In the. NET Framework, which means that the operators and methods for changing the string will return the modified copy of the string, which means there is still room for improvement in performance. When performing a large number of string operations, using the StringBuilder class is a good choice.
How does the StringBuilder class use ASP. NET skills? The following code tests the time required to generate a string from the 10000 substring in two ways. A simple String concatenation operation was used for the first time, and the StringBuilder class was used for the second time. To view the result string, remove the annotation symbol of the annotation line in the following code:
- & Lt; % @ Page Language ="C #"% & Gt;
-
- & Lt; script runat ="Server"& Gt;
- VoidPage_Load (Object Source, EventArgs E)
- {
- IntIntLimit = 10000;
- DateTime startTime;
- DateTime endTime;
- TimeSpan elapsedTime;
- StringStrSub;
- StringStrWhole ="";
-
- // First perform the string connection operation
- StartTime = DateTime. Now;
- For(IntI = 0; I & lt; intLimit; I ++)
- {
- StrSub = I. ToString ();
- StrWhole = strWhole +""+ StrSub;
- }
- EndTime = DateTime. Now;
- ElapsedTime = endTime-startTime;
- LblConcat. Text = elapsedTime. ToString ();
- // LblConcatString. Text = strWhole;
-
- // Use the stringBuilder class for the same operation
- StartTime = DateTime. Now;
- StringBuilder sb =NewStringBuilder ();
- For(IntI = 0; I & lt; intLimit; I ++)
- {
- StrSub = I. ToString ();
- Sb. Append ("");
- Sb. Append (strSub );
- }
- EndTime = DateTime. Now;
- ElapsedTime = endTime-startTime;
- LblBuild. Text = elapsedTime. ToString ();
- // LblBuildString. Text = sb. ToString ();
- }
-
- & Lt;/script & gt;
-
- & Lt; html & gt;
- & Lt; body & gt;
- & Lt; form runat ="Server"& Gt;
-
- & Lt; h1 & gt; String Concatenation Benchmark & lt;/h1 & gt;
-
- Concatenation:
- & Lt; asp: Label
- Id ="LblConcat"
- Runat ="Server"/& Gt;
-
- & Lt; br/& gt;
-
- & Lt; asp: Label
- Id ="LblConcatString"
- Runat ="Server"/& Gt;
-
- & Lt; br/& gt;
- & Lt; br/& gt;
-
- StringBuilder:
- & Lt; asp: Label
- Id ="LblBuild"
- Runat ="Server"/& Gt;
-
- & Lt; br/& gt;
-
- & Lt; asp: Label
- Id ="LblBuildString"
- Runat ="Server"/& Gt;
-
- & Lt;/form & gt;
- & Lt;/body & gt;
- & Lt;/html & gt;
The difference between the two methods is quite large: The Append method using the StringBuilder class is nearly 200 times faster than the string connection.
The usage of the StringBuilder class in ASP. NET skills will be introduced here, and I hope it will help you.
- Analysis on ASP. NET database connection pool settings
- How to Learn the nine steps of ASP. NET
- Analysis of data collection procedures using ASP. NET Techniques
- ASP. NET naming settings
- Analysis on ASP. NET technique to improve application performance