Performance testing between the string+ String.Concat String.Format StringBuilder

Source: Internet
Author: User

Find a foreign code, specifically to test this,

string+

String.Concat

String.Format

StringBuilder

The first three are about 100 characters in the same string,

String.Concat will get a little bit better performance improvements,

String.Format will make it easier for you to use.

StringBuilder more suitable for more and longer string stitching,

If there are other insights, please also guide.

usingSystem;usingSystem.Diagnostics;usingSystem.Text;namespacecompareinstructionexecutionspeed{ Public Delegate voidCompareexcecutionspeed (intloop); classProgram { Public Static stringResultconcatenation =string.        Empty;  Public Static ReadOnlyStringBuilder Sb =NewStringBuilder ();  Public Static ReadOnlyStopwatch Stopwatch =NewStopwatch ();  Public Static voidMain () {Compareexcecutionspeed methods=Stringbuilderexecutionspeed; Methods+=Stringconcatexecutionspeed; Methods+=Manualconcatenationexecutionspeed; Methods+=Stringformatexecutionspeed; //Methods+=some Method--can add your method to calculate speed.methods. Invoke ( -);//CountConsole.readkey (); }        //elapsing StringBuilder-------------------------------------------         Public Static voidStringbuilderexecutionspeed (intLoop)            {Stopwatch.restart ();  for(inti =0; I < loop; i++) {showpercentprogress (I, loop); Sb.append ("Str");            Sb.appendline (i.ToString ());            } stopwatch.stop (); Showcompareresult ("StringBuilder", Stopwatch); }        //elapsing str1+str2+ ...-------------------------------------------         Public Static voidManualconcatenationexecutionspeed (intLoop)            {Stopwatch.restart ();  for(inti =0; I < loop; i++) {showpercentprogress (I, loop); Resultconcatenation+="Str"+ i +"\ n";            } stopwatch.stop (); Showcompareresult ("str1+str2+ ...", Stopwatch); }        //elapsing String.Concat-------------------------------------------         Public Static voidStringconcatexecutionspeed (intLoop)            {Stopwatch.restart ();  for(inti =0; I < loop; i++) {showpercentprogress (I, loop); Resultconcatenation+=string. Concat ("Str"I"\ n");            } stopwatch.stop (); Showcompareresult ("String.Concat", Stopwatch); }        //elapsing String.Format-------------------------------------------         Public Static voidStringformatexecutionspeed (intLoop)            {Stopwatch.restart ();  for(inti =0; I < loop; i++) {showpercentprogress (I, loop); Resultconcatenation+=string. Format ("str{0}\n", i);            } stopwatch.stop (); Showcompareresult ("String.Format", Stopwatch); }        //Show Compare Result---------------------------------------------         Public Static voidShowcompareresult (stringmessage, Stopwatch Stopwatch)            {Console.resetcolor (); Console.WriteLine ("\r{0}\t{1,9} Millisecond ~={2,3} second ~={3,3} minutes", message, Math.Round (stopwatch. Elapsed.totalmilliseconds), Math.Round (stopwatch. Elapsed.totalseconds), Math.Round (stopwatch.        Elapsed.totalminutes)); }        //Show Processing Progress----------------------------------------        Static voidShowpercentprogress (intCurrelementindex,intTotalelementcount) {Console.foregroundcolor=Consolecolor.green; intPercent = ( -* (Currelementindex +1)) /Totalelementcount; Console.Write ("\r{0}%", percent); }    }}

Performance testing between the string+ String.Concat String.Format StringBuilder

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.