Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can you please let me know which has better performance
StringBuilder.AppendLine
or
StringBuilder.Append

I want to write html code from server side, the html code is more than 10,000 lines.
Which option should i prefer ?

Thanks in advance.
Posted

If you will go through StringBuilder.Append Method (String)[^], you will find that

Append
Appends a copy of the specified string to this instance.

Finally:
ToString returns the buffer. You will almost always want ToString—it will return the contents as a string.

And after referring C# StringBuilder AppendLine[^] and StringBuilder.AppendLine Method (String)[^], you will come to know that

AppendLine
Appends a copy of the specified string followed by the default line terminator to the end of the current StringBuilder[^] object.

Optimization
There are two common ways to specify newlines: the character "\n" alone, and the sequence "\r\n". AppendLine always uses the sequence "\r\n". To save bytes from the output string, you can manually use "\n". This means every line in your text output will be one character shorter. In ASCII format, this will save one byte per line.

Info:
Your C# program will actually save two bytes per line in its memory, because a character is two bytes long.
So, you have to decide as per your requirement. Use Appendline[^], if it is really necessary, otherwise ignore.

As you are dealing with html contents, so if you need to show it formatted, then you need to use it and show the html contents line by line clearly.

If you just want to save that html data and no need to show it, then you should go for Append, as you will save some memory, which gets lost due to the new line characters "\r\n".

References
1. StringBuilder.Append Method[^]
2. StringBuilder.AppendLine Method[^]
3. StringBuilder.Append Method (String)[^]
4. StringBuilder.AppendLine Method (String)[^]

Thanks...
 
Share this answer
 
v3
Comments
Hi @Turbo_23,

Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Tadit
[no name] 15-Jan-13 3:37am    
Thanks a lot :-)
Most Welcome. My pleasure.
fjdiewornncalwe 15-Jan-13 10:06am    
You have a serious issue in your answer. Append does not call AppendLine. AppendLine calls Append and then attaches an Environment.NewLine to that.
Hi @Marcus,

You are correct. I just added the solution, but after your comment, I checked again and banged my head as I was so stupid.

Thanks for the information and help.

Now, I have updated the solution, so that you will consider this for upvoting. :P :P :P

Thanks,
Tadit
Try it! It's not difficult:
C#
void myButton_Click(object sender, EventArgs e)
    {
    DoAppend();
    DoAppendLine();
    }
void DoAppend()
    {
    Stopwatch s = new Stopwatch();
    s.Start();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 1000000; i++)
        {
        sb.Append("hello there");
        sb.Append("\n");
        }
    s.Stop();
    Console.WriteLine(s.ElapsedMilliseconds);
    }
void DoAppendLine()
    {
    Stopwatch s = new Stopwatch();
    s.Start();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 1000000; i++)
        {
        sb.AppendLine("hello there");
        }
    s.Stop();
    Console.WriteLine(s.ElapsedMilliseconds);
    }
When I did I got these results:
VB
61
82

73
69

76
79

69
76
Which shows that in practice, they are actually pretty much the same. The difference in timings will be due to other things happening in the system, and that the highest can reverse from Append to AppendLine just shows they are pretty identical.
 
Share this answer
 
Comments
[no name] 15-Jan-13 3:36am    
thanks a lot :-)
OriginalGriff 15-Jan-13 3:50am    
You're welcome!
fjdiewornncalwe 15-Jan-13 10:04am    
+5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900