Three methods and efficiency for clearing stringbuilder

Source: Internet
Author: User

Three methods and efficiency for clearing stringbuilder   We know that stringbuilder is used for frequent String concatenation. the append method is much more efficient than the string + = method, but sometimes you do not know how to clear stringbuilder because it does not have the clear or empty method. So what method is used? I searched the internet for three methods. 1. Remove example: stringbuilder val = new stringbuilder (); Val. append (".... "); Val. remove (0, Val. length); // clear 2. Replace stringbuilder val = new stringbuilder (); Val. append (".... "); Val. replace (Val. tostring (), ""); // clear 3. Length stringbuilder val = new stringbuilder (); Val. append (".... "); Val. length = 0; // empty the efficiency of the three methods is simply tested, and the average value of the three methods is tested: datetime dt = datetime. now; const int testnum= 1000000; stringbuilde R strbuilder = new stringbuilder (); For (INT I = 0; I <testnum; I ++) {strbuilder. append ("test"); strbuilder. remove (0, strbuilder. length);} console. writeline ("rsemove:" + (datetime. now-Dt ). ticks); first, time used: 366687; second, time used: 1186734; third, time used: 180010, visible, or length used, which is more efficient. This test is my simple test. If you have different opinions, please leave a message to study and discuss it together!

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.