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