StringBuilder as a member of a class?

Is it efficient (or necessary?) To make a StringBuildermember of a class and use it in class methods whenever string concatenation is performed?

Or just use the new one StringBuidler()before such operations?

I suspect it is effective, if any StringBuilder, ready for use there.

Syn

+4
source share
1 answer

This is an interesting question, and one that has been investigated .

: , , StringBuilder.

, , , , . , , , , .

:

using System;
using System.Diagnostics;
using System.Text;

class Program
{
    static string Method1()
    {
        StringBuilder builder = new StringBuilder();
        foreach (string value in _array)
            builder.Append(value);
        return builder.ToString();
    }

    static StringBuilder _builder = new StringBuilder();

    static string Method2()
    {
        StringBuilder builder = _builder;
        builder.Clear();
        foreach (string value in _array)
            builder.Append(value);
        return builder.ToString();
    }

    static string[] _array = { "dot", "net", "perls", "string", "array" };
    const int _max = 1000000;
    static void Main()
    {
        Method1();
        Method2();

        // Version 1: use new StringBuilder each time.
        var s1 = Stopwatch.StartNew();
        for (int i = 0; i < _max; i++)
        {
            Method1();
        }
        s1.Stop();

        // Version 2: reuse a single StringBuilder instance.
        var s2 = Stopwatch.StartNew();
        for (int i = 0; i < _max; i++)
        {
            Method2();
        }
        s2.Stop();
        Console.WriteLine(s1.Elapsed.TotalMilliseconds);
        Console.WriteLine(s2.Elapsed.TotalMilliseconds);
        Console.Read();
    }
}

( ):

  • : 146.21
  • StringBuilder : 98.96
+4

Source: https://habr.com/ru/post/1619349/


All Articles