Stringbuild,stringformat, "+" "three Methods of speed test

Source: Internet
Author: User

Test method:

Stopwatch SW =NewStopwatch (); SW. Start ();stringTMP =""; StringBuilder SB=NewStringBuilder (); for(inti =0; I <10000; i++){    //tmp + = "|" + i;Sb. Append ("|"); Sb.    Append (i.ToString ()); //tmp = string. Format ("{0}{1}{2}", tmp, "|", i);}SW. Stop (); Console.WriteLine (sb.) ToString ()); Console.WriteLine (SW. Elapsedmilliseconds); Console.read ();
View Code

Create a For loop of 10,000 times, add the number of I and divide it by |

Different types of combinations StringFormat will be slightly faster than + +. But the combination is more slow than the +.

3 parameters , and different types or complex format with stringformat suitable. (A long string is slower than "+")

More than 3 parameters, the same type is more, with +.

More complex considerations Stringbuild

Name Speed
Stringbuild 2ms
StringFormat 655ms
+ and StringFormat mates 172ms
+ 181ms

Note: + and StringFormat mates refer to: tmp + = string. Format ("{0}{1}", "|", I);

Stringbuild,stringformat, "+" "three Methods of speed test

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.