最快的System.IO方法? [英] Fastest System.IO method?

查看:67
本文介绍了最快的System.IO方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



到目前为止我从未对System.IO的性能提出过质疑,但我已经做了一些测试,并意识到写的有多么糟糕和令人沮丧一个简单的文本文件。



我使用这个简单的代码来检查文件是否可用,然后只需在文本文件中附加一行。



Hi everyone,

to this day i never quite questioned System.IO performance, but i have done some testing and realized the how badly and frustrating is to write a simple text file.

I am using this simple code to check if file is available for use and then just append a line to a text file.

public void _AppendData(string _path, string _text)
{
     lock (locker)
     {
          /* Append request log to master log file */
          FileInfo fi = new FileInfo(_path);

          using (FileStream file = new FileStream(fi.FullName, FileMode.Append, FileAccess.Write, FileShare.Read))
          {
              using (StreamWriter writer = new StreamWriter(file))
                  {
                      writer.Write(_text);
                  }
          }
      }
}





我创建了一个简单的循环写10000行文本这是第x行。到一个文件,总共花了00:02:36来做。请注意,我有一台功能强大的机器,配备32个内存和英特尔的I7芯片。现在我知道IO依赖于它自己的磁盘驱动器,因此固态硬盘在这里会有不同的表现。



因为这段代码非常适合我需要它我是否乐意将其保留原样,但是我很想知道在标准驱动器上是否有更快的方法可以在一分钟内完成同样的工作?



I have created a simple loop to write 10000 lines of text "This is line x." to a single file which took 00:02:36 in total to do. Mind you i have a capable machine here with 32 gigs of RAM and Intel's I7 chip. Now i know that IO depends on the disk drive it self therefore a Solid State Drive would perform differently here.

As this code works quite well for what i need it to do i am happy to leave it as it is, however i am very curious to know if there is a faster way to do the same job for under a minute on standard drive?

推荐答案

如果您必须随时写下记录,我可以考虑对原始循环进行两处更改,以显着改善其性能。



1)保持文件打开,并将 StreamWriter 传递到 _AppendData 方法。



2)将 StreamWriter 上的缓冲区属性设置为 8192 字节的顺序。



多年来,我发现8KB是缓冲区大小的最佳位置,无论编程语言或平台如何。虽然我已经使用了几年,但是我使用相同的代码执行了定时测试,只改变了缓冲区的大小。在每种情况下,当我将缓冲区从256字节增加到8192字节时,性能显着提高。除此之外,对性能的影响可以忽略不计。
If you must write records as you go, I can think of two changes to your original loop that should significantly improve its performance.

1) Keep the file open, and either pass the StreamWriter into your _AppendData method.

2) Set the buffer property on the StreamWriter to something on the order of 8192 bytes.

Over many years, I have found that 8KB is the sweet spot for buffer size, regardless of programming language or platform. Though it has been several years since I did so, I have performed timed tests, using identical code, varying only the size of the buffer. In every case, performance improved significantly as I increased the buffer from 256 bytes to 8192 bytes. Beyond that, the effect on performance was negligible.


性能问题在于处理循环中的数据。此代码已设法将处理时间从2分钟缩短到2秒。



The problem with performance was in handling the data in the loop. This code has managed to reduce processing from over 2 minutes to just 2 seconds.

string _path = @"C:\Output.txt";
string _text = string.Empty;

for (int x = 0; x < 10000; x++)
{
    _text = _text + "This is line " + x.ToString() + Environment.NewLine;
}

_AppendDataFile(_path, _text);


这篇关于最快的System.IO方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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