C # a small example of using stringbuilder to Improve the Performance of String concatenation

Source: Internet
Author: User

I monitored it using stopwatch in segments and found that the most time-consuming function was savetoexcel.

This function is used to repeat all data rows in the column, replace the tag to generate an Excel row, and then accumulate the row data to a string.

CopyCodeThe Code is as follows: String excelstring = "";
Foreach (VAR item in list ){
Excelstring + = string. Format ("<row>... {0} </row>", list. Title );
}

See this .. I immediately remembered the warnings of countless martyrs and the memory operating principles of String concatenation. As a result, the code was completely deleted and changed to the following format:Copy codeThe Code is as follows: String excelstring = new stringbuilder ();
Foreach (VAR item in list ){
Excelstring. appendformat ("<row>... {0} </row>", list. Title );
}

The test again immediately increased the efficiency by dozens of times. The first 8000 rows require 30 s, and now only 2 S

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.