A small example of C # using StringBuilder to promote string concatenation performance

Source: Internet
Author: User
Tags foreach

Monitored with stopwatch, the most time-consuming function was found Savetoexcel

This function lists all the rows of data, builds the Excel row with the Replace tag, and assigns the row data to a string

Copy Code code as follows:
String excelstring = "";
foreach (var item in list) {
excelstring = = String. Format ("<row> .....") {0}</row> ", list. Title);
}


See this. Immediately think of countless martyrs of the warning, string concatenation of memory operation principle and so on. So the big hand a wave, the code all deleted, change to the following form

Copy Code code as follows:
String excelstring = new StringBuilder ();
foreach (var item in list) {
Excelstring.appendformat ("<ROW> {0}</row> ", list. Title);
}


Again, the efficiency was increased dozens of times times. The previous 8000 lines need 30s and now only need 2s

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.