什么消耗更少的资源,更快File.AppendText或File.WriteAllText存储第一的StringBuilder的?更快、更少、消耗、资源

2023-09-03 01:08:57 作者:携酒天涯

我要写数千动态生成行到一个文本文件中。 我有两个选择,消耗更少的资源,是比其他快?

一个。使用StringBuilder的和File.WriteAllText

  StringBuilder的SB =新的StringBuilder();
的foreach(数据的DataItem在DATAS)
{
sb.AppendLine(的String.Format({0},{1}  -  {2},DataItem的.Property1,DataItem的.Property2,DataItem的.Property3));
}
File.WriteAllText(C:\\ example.txt的,sb.ToString(),新UTF8Encoding(假));
 

乙。使用File.AppendText

 使用(StreamWriter的SW = File.AppendText(C:\\ example.txt文件)){
                                的foreach(数据的DataItem在DATAS)
                                {
                                    sw.WriteLine(的String.Format({0},{1}  -  {2},DataItem的.Property1,DataItem的.Property2,DataItem的.Property3));
                                }
                            }
 

解决方案 IPFS FIL反弹,存储需求增加10月前还有一波拉升,你手上有多少币

您的第一个版本,这使一切都变成的StringBuilder ,然后将其写入,会消耗内存最多。如果文字是非常大的,你必须运行内存的潜力。它有可能要快,但它也可以是慢

第二个选项将使用更少的内存(基本上,只是的StreamWriter 缓存),并会表现非常好。我会建议使用此选项。它表现良好 - 可能比第一方法更好 - 并且不具有相同的电势为耗尽存储器

您可以通过增加输出缓冲区的大小,速度也相当多。而不是

  File.AppendText(文件名)
 

创建具有流:

  const int的缓冲区大小= 65536; // 64千字节
StreamWriter的SW =新的StreamWriter(文件名,真实,Encoding.UTF8,缓冲区大小);
 

64K的缓冲器大小给出比默认4K缓冲器大小更好的性能。你可以去更大,但我发现,大于64K提供最低限度的性能提升,而且在某些系统上可以真正的降低的性能。

I have to write thousands of dynamically generated lines to a text file. I have two choices, Which consumes less resources and is faster than the other?

A. Using StringBuilder and File.WriteAllText

StringBuilder sb = new StringBuilder();
foreach(Data dataItem in Datas)
{
sb.AppendLine(String.Format("{0}, {1}-{2}",dataItem .Property1,dataItem .Property2,dataItem .Property3));
}
File.WriteAllText("C:\\example.txt", sb.ToString(), new UTF8Encoding(false)); 

B. Using File.AppendText

using(StreamWriter sw = File.AppendText("C:\\example.txt")){
                                foreach (Data dataItem in Datas)
                                {
                                    sw.WriteLine(String.Format("{0}, {1}-{2}",dataItem .Property1,dataItem .Property2,dataItem .Property3));
                                }
                            }

解决方案

Your first version, which puts everything into a StringBuilder and then writes it, will consume the most memory. If the text is very large, you have the potential of running out of memory. It has the potential to be faster, but it could also be slower.

The second option will use much less memory (basically, just the StreamWriter buffer), and will perform very well. I would recommend this option. It performs well--possibly better than the first method--and doesn't have the same potential for running out of memory.

You can speed it quite a lot by increasing the size of the output buffer. Rather than

File.AppendText("filename")

Create the stream with:

const int BufferSize = 65536;  // 64 Kilobytes
StreamWriter sw = new StreamWriter("filename", true, Encoding.UTF8, BufferSize);

A buffer size of 64K gives much better performance than the default 4K buffer size. You can go larger, but I've found that larger than 64K gives minimal performance gains, and on some systems can actually decrease performance.