StringBuilder.AppendLine与StringBuilder.Append的性能 [英] Performance of StringBuilder.AppendLine vs StringBuilder.Append

查看:147
本文介绍了StringBuilder.AppendLine与StringBuilder.Append的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





能告诉我哪个有更好的表现

Hi,

Can you please let me know which has better performance

StringBuilder.AppendLine

或者

StringBuilder.Append



我想从服务器端编写html代码,html代码超过10,000行。

我更喜欢哪个选项?



提前致谢。


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.

推荐答案

试试吧!这并不难:

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

这表明在实践中,它们实际上几乎相同。时间上的差异将归因于系统中发生的其他事情,并且最高可以从Append到AppendLine反转只是显示它们非常相同。

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.


如果你将经历 StringBuilder.Append方法(字符串) [ ^ ],你会发现

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


追加

将指定字符串的副本附加到此实例。



最后:

ToString返回缓冲区。你几乎总是想要ToString - 它会将内容作为字符串返回。


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.



在引用C# StringBuilder AppendLine [ ^ ]和 StringBuilder.AppendLine Method(String) [ ^ ],你会发现


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



AppendLine

将指定字符串的副本后跟默认行终止符附加到当前 StringBuilder [ ^ ]对象。



< I>优化中n

指定换行符有两种常用方法:单独使用字符\ n,以及序列\\\\ n。 AppendLine始终使用序列\\\\ n。要从输出字符串保存字节,可以手动使用\ n。这意味着文本输出中的每一行都会缩短一个字符。在ASCII格式中,这将节省每行一个字节。



信息:

你的C#程序实际上每行节省两个字节内存,因为一个字符长两个字节。


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.

所以,你必须根据你的要求来决定。使用附录 [ ^ ],如果确实有必要,否则忽略。



当你处理html内容时,如果你需要显示它格式化,然后你需要使用它并清楚地逐行显示HTML内容。



如果你只是想保存那些HTML数据而不需要显示它,那么你应该去追加,因为你会节省一些内存,由于新的行字符\\\\ n而丢失。



< b> 参考文献

1. StringBuilder.Append方法 [ ^ ]

2. StringBuilder.AppendLine方法 [ ^ ]

3. StringBuilder.Append Method(String) [ ^ ]

4. StringBuilder.AppendLine方法(字符串) [ ^ ]



谢谢......

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...


这篇关于StringBuilder.AppendLine与StringBuilder.Append的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆