Click here to Skip to main content
11,639,861 members (62,819 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 14-Jan-13 20:20pm
Adi_2.3K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try it! It's not difficult:
        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:
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.
  Permalink  
Comments
Turbo_23 at 15-Jan-13 3:36am
   
thanks a lot :-)
OriginalGriff at 15-Jan-13 3:50am
   
You're welcome!
Marcus Kramer at 15-Jan-13 10:04am
   
+5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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...
  Permalink  
v3
Comments
Tadit Dash at 15-Jan-13 3:18am
   
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
Turbo_23 at 15-Jan-13 3:37am
   
Thanks a lot :-)
Tadit Dash at 15-Jan-13 5:22am
   
Most Welcome. My pleasure.
Marcus Kramer at 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.
Tadit Dash at 16-Jan-13 1:32am
   
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

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

  Print Answers RSS
0 Suvendu Shekhar Giri 270
1 CPallini 177
2 F-ES Sitecore 170
3 jyo.net 142
4 himanshu agarwal 142
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,847
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,650
4 F-ES Sitecore 2,558


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100