FileStream.BeginWrite advantages over FileStream.Write?

后端 未结 1 1446
既然无缘
既然无缘 2021-01-02 23:43

I need to make a batch of writes to the same file but at different places within the file. I want to achieve this with the best performance possible and so have looked at th

相关标签:
1条回答
  • 2021-01-03 00:16

    File writes are heavily optimized in Windows. You don't actually write to the disk, you write to the file system cache. A memory-to-memory copy, runs at 5 gigabytes per second or better. From the cache, the data is then lazily written to disk. In turn optimized to minimize the number of write head moves.

    This is next to impossible to optimize with asynchronous writes. Which do indeed take longer, grabbing the threadpool thread to make the callback doesn't come for free. The benefit of async here is minimizing the main thread delays, not to actually make it more efficient. You will only actually get the benefit when you write very large amounts of data. More than will fit into the cache. At that point, write perf will fall off a cliff from 5 GB/sec to less than ~50 MB/sec since cache space can only become available at the rate the disk can be written.

    Exactly when that happens is hard to predict. It depends how much RAM the machine has and how much of it is needed by other processes. You basically don't worry about it when you write a gigabyte or less. And it is important that you actually have something useful to do when the async write is pending. Waiting for them to complete defeats the point of using it.

    0 讨论(0)
提交回复
热议问题