StringBuilder performance in C #?

I have an object StringBuilderwhere I add a few lines, for example:

I want to know which one is better here, the first one:

StringBuilder sb = new StringBuilder();
sb.Append("Hello" + "How" + "are" + "you");

and second:

StringBuilder sb = new StringBuilder();
sb.Append("Hello").Append("How").Append("are").Append("you");
+5
source share
5 answers

The first will be more effective. The compiler will convert it to the following single call:

StringBuilder sb = new StringBuilder();
sb.Append("HelloHowareyou");

performance measurement

The best way to find out which is faster is to measure it. I'll go right away: here are the results (less time means faster):

sb.Append("Hello" + "How" + "are" + "you")                  : 11.428s
sb.Append("Hello").Append("How").Append("are").Append("you"): 15.314s
sb.Append(a + b + c + d)                                    : 21.970s
sb.Append(a).Append(b).Append(c).Append(d)                  : 15.529s

The number indicated is the number of seconds to complete an operation 100 million times in a compressed cycle.

findings

  • The fastest are string literals and +.
  • , Append , +. - String.Concat.

, , :

using System;
using System.Text;

public class Program
{
    public static void Main()
    {
        DateTime start, end;
        int numberOfIterations = 100000000;
        start = DateTime.UtcNow;
        for (int i = 0; i < numberOfIterations; ++i)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Hello" + "How" + "are" + "you");
        }
        end = DateTime.UtcNow;
        DisplayResult("sb.Append(\"Hello\" + \"How\" + \"are\" + \"you\")", start, end);

        start = DateTime.UtcNow;
        for (int i = 0; i < numberOfIterations; ++i)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Hello").Append("How").Append("are").Append("you");
        }
        end = DateTime.UtcNow;
        DisplayResult("sb.Append(\"Hello\").Append(\"How\").Append(\"are\").Append(\"you\")", start, end);

        string a = "Hello";
        string b = "How";
        string c = "are";
        string d = "you";

        start = DateTime.UtcNow;
        for (int i = 0; i < numberOfIterations; ++i)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(a + b + c + d);
        }
        end = DateTime.UtcNow;
        DisplayResult("sb.Append(a + b + c + d)", start, end);

        start = DateTime.UtcNow;
        for (int i = 0; i < numberOfIterations; ++i)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(a).Append(b).Append(c).Append(d);
        }
        end = DateTime.UtcNow;
        DisplayResult("sb.Append(a).Append(b).Append(c).Append(d)", start, end);

        Console.ReadLine();
    }

    private static void DisplayResult(string name, DateTime start, DateTime end)
    {
        Console.WriteLine("{0,-60}: {1,6:0.000}s", name, (end - start).TotalSeconds);
    }
}
+6

:

"Hello" + "How" + "are" + "you"

, , :

sb.Append("Hello").Append("How").Append("are").Append("you");

, :

sb.Append(s1 + s2 + s3 + s4);

, (- ) Append, ( ).

:. , 4 , , String.Concat(string, string, string, string), , StringBuilder.

+10

. , String.Concat

s + t + u + v ==> String.Concat(s, t, u, v)

, StringBuilder, StringBuilder , Concat . , , StringBuilder,

var sb = new StringBuilder(initialBufferSize);

StringBuilder , s += t .

+4

, . , . ?

0

- . , , sb.Append( "Hello" + "How" + "Are" + "You" )

.

"Hello"

"HelloHow"

"HelloHowAre"

.

edit: , ,

, , , , , - , StringBuilder

, :

var someString = "";

foreach (var s in someListOfStrings) 
{
    someString += s;
}

, :

var sb = new StringBuilder();

foreach(var s in someListOfStrings)
{
    sb.Append(s);
}

sb.ToString();

, , , , ,

, OP ,

sb.Append("Hello" + "How");

,

sb.Append("HelloHow");

It would be more logical ...?

It seems to me that in the minds of OPs, placeholder text will eventually become a transfer of variables ...

-5
source

All Articles