What is the fastest way to read / write to disk in .NET?

I have a small program that reads and writes files to disk. Breaking it to the simplest level, it reads bytes from one stream of files and writes them to another. He performs his duties well, but this is not the fastest thing.

I have seen other applications that can break through gigabytes or more read / write with amazing speed. Obviously, they work closer to metal than a small .NET application.

What are the most efficient .NET APIs for streaming to / from disk? What win32 APIs are available (and are p / invoking for) for quick disk access?

+5
source share
4 answers

Fast file input / output is less relevant to the specific API calls you make, but rather about how you archive your I / O application.

If you perform all the I / O on a single thread in a sequential manner, for example

  • Reading a block into memory
  • The processing unit in memory is somehow
  • Write to file
  • Repeat until the end ...

You are experiencing system I / O throughput in a single thread processing cycle. An alternative but more sophisticated design is a multi-threaded application to maximize throughput and avoid latency. This allows the system to simultaneously use both processor bandwidth and I / O controller bandwidth. A typical design for this would look something like this:

  • ( )
  • ( ) ,
  • ( ) , , .

, -. , , /. , , - , . , , .

, / - API - .

.

+10

.NET ( Win32). , :

+6

, , - ?

? ?

.NET System.IO.File.

Win32 CreateFile, WriteFile, ReadFile series.

:

http://msdn.microsoft.com/en-us/library/bb540534(VS.85).aspx

. .

+1

BinaryReaderand BinaryWriterwith a suitable buffer size pretty fast. If you read structures, the unsafe approach described in this article will force you to read quickly, and the record is similar. I also agree with the suggestion to double check that I / O is indeed a bottleneck. I first came across this article because of such an error.

0
source

All Articles