Console.Write(i) in each for or Console.write(Stringbuilder) 最后哪个更好

Console.Write(i) in each for or Console.write(Stringbuilder) at the end which one is better

Console.Write(i)在每个forConsole.Write(StringBuilder)结尾:哪个更好?

我有两个函数,第一个在 for 循环内打印,另一个在最后打印。

public static void checkmethod1(int value, Checkevent text)
{
    Stopwatch stopwatch2 = new Stopwatch();
    stopwatch2.Start();
    StringBuilder builder = new StringBuilder();

    switch (text)
    {
        case Checkevent.odd:
            for (int i = 1; i <= value; i = i + 2)
            {
                builder.Append(i).Append(" ");
            }
            break;

        case Checkevent.even:
            for (int i = 2; i <= value; i = i + 2)
            {
                builder.Append(i).Append(" ");
            }
            break;
    }
    stopwatch2.Stop();
    Console.WriteLine(builder);
    Console.WriteLine("{0}", stopwatch2.Elapsed);

}

函数 2:

public static void checkmethod3(int value, Checkevent text)
{
    Stopwatch stopwatch2 = new Stopwatch();
    stopwatch2.Start();

    switch (text)
    {
        case Checkevent.odd:
            for (int i = 1; i <= value; i = i + 2)
            {
                Console.Write(i);
            }
            break;
        case Checkevent.even:
            for (int i = 2; i <= value; i = i + 2)
            {
                Console.Write(i);
            }
            break;
    }
    stopwatch2.Stop();
    Console.Write("{0}", stopwatch2.Elapsed);
}

在这种特定情况下,我更喜欢 StringBuilder,因为该循环不会占用可能改变用户体验的大量时间。 StringBuilder 通常需要更少的内存,您将获得更好的性能。当您创建字符串新 string 对象中的每个修改时,但 StringBuilder.

并非如此

第一个方法只执行 Console.Write 一次 一次 但第二个方法将执行 for 循环迭代。这将使第二个变慢。

如果您想在控制台中向用户显示文本,而您希望用户看到文本,就像您正在显示日志以查看流程一样,那么显示一次(StringBuilder)可能不会给用户有机会阅读它。在那种情况下,您将写入使用 Console.Write(string).

生成的日志

当您了解两者的工作原理后,决定何时使用 string 和何时使用 StringBuilder 会变得容易。他们的重要行为之一如下。

Using the StringBuilder Class in the .NET Framework

The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

编辑

我用三个值 100、10000 和 100000 测试了上述两种方法,使用的机器具有以下规格

Operating System: Windows 7 Enterprise and CPU
Processor: Intel(R) Core (TM) i5-3570 CPU @ 3.40 GHz 3.40 GHz
Installed memory (RAM): 8.00 GB
System Type: 64 bit Operating System

值 100

Time with StringBuilder 00:00:00.0000184
Time without StringBuilder 00:00:00.0037037

值 10000

Time with StringBuilder 00:00:00.0013233
Time without StringBuilder 00:00:00.2970272

值 100000

Time with StringBuilder 00:00:00.0133015
Time without StringBuilder 00:00:02.5853375

在您使用 StringBuilder 的第一种方法中,Console.Write 仅执行一次,但在其他情况下,它会执行与循环迭代一样多的次数。这使得第二个变慢。 将 StringBuilder 与字符串连接进行比较不适用于此处。