Use of StringBuilder class in ASP. NET skills

Source: Internet
Author: User

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:

 
 
  1. & Lt; % @ Page Language ="C #"% & Gt;
  2.  
  3. & Lt; script runat ="Server"& Gt;
  4. VoidPage_Load (Object Source, EventArgs E)
  5. {
  6. IntIntLimit = 10000;
  7. DateTime startTime;
  8. DateTime endTime;
  9. TimeSpan elapsedTime;
  10. StringStrSub;
  11. StringStrWhole ="";
  12.  
  13. // First perform the string connection operation 
  14. StartTime = DateTime. Now;
  15. For(IntI = 0; I & lt; intLimit; I ++)
  16. {
  17. StrSub = I. ToString ();
  18. StrWhole = strWhole +""+ StrSub;
  19. }
  20. EndTime = DateTime. Now;
  21. ElapsedTime = endTime-startTime;
  22. LblConcat. Text = elapsedTime. ToString ();
  23. // LblConcatString. Text = strWhole; 
  24.  
  25. // Use the stringBuilder class for the same operation 
  26. StartTime = DateTime. Now;
  27. StringBuilder sb =NewStringBuilder ();
  28. For(IntI = 0; I & lt; intLimit; I ++)
  29. {
  30. StrSub = I. ToString ();
  31. Sb. Append ("");
  32. Sb. Append (strSub );
  33. }
  34. EndTime = DateTime. Now;
  35. ElapsedTime = endTime-startTime;
  36. LblBuild. Text = elapsedTime. ToString ();
  37. // LblBuildString. Text = sb. ToString (); 
  38. }
  39.  
  40. & Lt;/script & gt;
  41.  
  42. & Lt; html & gt;
  43. & Lt; body & gt;
  44. & Lt; form runat ="Server"& Gt;
  45.  
  46. & Lt; h1 & gt; String Concatenation Benchmark & lt;/h1 & gt;
  47.  
  48. Concatenation:
  49. & Lt; asp: Label
  50. Id ="LblConcat"
  51. Runat ="Server"/& Gt;
  52.  
  53. & Lt; br/& gt;
  54.  
  55. & Lt; asp: Label
  56. Id ="LblConcatString"
  57. Runat ="Server"/& Gt;
  58.  
  59. & Lt; br/& gt;
  60. & Lt; br/& gt;
  61.  
  62. StringBuilder:
  63. & Lt; asp: Label
  64. Id ="LblBuild"
  65. Runat ="Server"/& Gt;
  66.  
  67. & Lt; br/& gt;
  68.  
  69. & Lt; asp: Label
  70. Id ="LblBuildString"
  71. Runat ="Server"/& Gt;
  72.  
  73. & Lt;/form & gt;
  74. & Lt;/body & gt;
  75. & 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.

  1. Analysis on ASP. NET database connection pool settings
  2. How to Learn the nine steps of ASP. NET
  3. Analysis of data collection procedures using ASP. NET Techniques
  4. ASP. NET naming settings
  5. Analysis on ASP. NET technique to improve application performance

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.