Shirokuma1205
Shirokuma1205

Reputation: 61

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

Console.Write(i) in each for or Console.Write(StringBuilder) at the end: Which one is better?

I have two functions, the first one prints within the for loop and the other one is printing at the end.

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);

}

Function 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);
}

Upvotes: 2

Views: 1186

Answers (1)

Adil
Adil

Reputation: 148110

In this particular scenario I will prefer StringBuilder as The loop is not taking significant time that could change the user experience. The StringBuilder in general require less memory and you will get better performance. As you each modification in string new string object is create but that is not the case with StringBuilder.

The first method executed the Console.Write only once but the second on will execute it the times for loop iterates. This will make the second one slow.

If you want to show user the text in console when you want user to see text as it appear like you are showing log to view the process flow then showing it once (with StringBuilder) may not give user chance to read it. In that case you would be write log as it is generated using Console.Write(string).

Deciding when to use string and when to use StringBuilder could become easy when you understand how both work. Their one of important behavior is given as under.

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.

Edit

I test the above two method with three values 100, 10000 and 100000 and machine used has following specs

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

Value 100

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

Value 10000

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

Value 100000

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

In the first method where you used the StringBuilder the Console.Write is executed only once but in other case it is executed as many times as the loop iterations. This makes the second one slow. Comparing StringBuilder with string concatenation is not applicable here.

Upvotes: 3

Related Questions