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