asp.net (C #) String, StringBuilder and StringWriter Performance Comparison _ Practical Tips

Source: Internet
Author: User
Intuitive understanding: Head-on confrontation
Performance Test 1:stringbuilder
1th round test: spents 312.5 milliseconds
2nd round test: spents 421.875 milliseconds
3rd round test: spents 453.125 milliseconds
4th round test: spents 421.875 milliseconds
5th round test: spents 453.125 milliseconds
Performance Test 2:stringwriter
1th round test: spents 406.25 milliseconds
2nd round test: spents 453.125 milliseconds
3rd round test: spents 421.875 milliseconds
4th round test: spents 437.5 milliseconds
5th round test: spents 437.5 milliseconds
Performance Test 3:string (1/100 data volume)
1th round test: spents 12406.25 milliseconds

Did you notice?
The String connection mode is 30 times times more than StringBuilder under test with only 1/100 data. Therefore, based on performance considerations, we do not recommend this approach. and StringBuilder compared to stringwriter slightly, the specific reasons will be analyzed in the next article. Of course, the tests are error-proof, but they are sufficient to illustrate the facts.

StringWriter and StringBuilder: Who is the strong
StringWriter is located within the System.IO namespace and inherits from TextWriter. Shown in the back-compilation result of the. Net Reflector, its interior is actually connected by StringBuilder. No wonder StringWriter will be slightly inferior, it turns out to be just an adaptation of StringBuilder (which can be called Adapter mode). Why is StringBuilder so efficient?

Did you notice?
In many places, StringWriter rather than StringBuilder, such as XmlTextWriter, are needed.

StringBuilder: why
On the research of System.Text.StringBuilder, there are many online, the main principle is to allocate the memory in an unmanaged way, guarantee the modification and expansion of the text, do not re-create a String object. The creation of a String object is a performance bottleneck. Its connection efficiency is far greater than string, but it is obvious that string programming is easier when a small amount of text is connected.
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.