Read File Fast

G

Guest

Hi,

Which one is the fast way to open a file and read it. If possible
without buffers and any other kind of conversion. I need one step from the
harddisk to the Array.

Right now I am using FileStream. Is this the best way?

Thank you,
Roby Eisenbraun Martins
 
J

Jon Skeet [C# MVP]

Roby Eisenbraun Martins
Which one is the fast way to open a file and read it. If possible
without buffers and any other kind of conversion. I need one step from the
harddisk to the Array.

Right now I am using FileStream. Is this the best way?

There are always going to be buffers at some level, but using
FileStream is likely to be as fast as you'll get in .NET. Do you have
any evidence that file reading is the bottleneck in your application?
Have you worked out how much slower your use of FileStream is than the
theoretical maximum transfer rate of your disk?
 
W

Willy Denoyette [MVP]

Roby Eisenbraun Martins said:
Hi,

Which one is the fast way to open a file and read it. If possible
without buffers and any other kind of conversion. I need one step from the
harddisk to the Array.

Right now I am using FileStream. Is this the best way?

Thank you,
Roby Eisenbraun Martins

It will depend on the drive configuration, FileStream goes pretty fast on
single drives, but using striped disk arrays, you get better results when
doing un-buffered IO.
This can be achieved by using PInvoke to call CreateFile with the
FILE_FLAG_NO_BUFFERING flag specified, the handle returned can simply be
passed as argument to one of the FileStream constructors taking an handle to
a File.

Willy.
 
G

Guest

So is possible, but only using Windows API?

Willy Denoyette said:
It will depend on the drive configuration, FileStream goes pretty fast on
single drives, but using striped disk arrays, you get better results when
doing un-buffered IO.
This can be achieved by using PInvoke to call CreateFile with the
FILE_FLAG_NO_BUFFERING flag specified, the handle returned can simply be
passed as argument to one of the FileStream constructors taking an handle to
a File.

Willy.
 
W

Willy Denoyette [MVP]

Roby Eisenbraun Martins said:
So is possible, but only using Windows API?

Yep, but there is only the CreateFile API to "PInvoke", note that the FCL
also uses PInvoke to call the Win32 API's for file IO.
Note also that while you bypass the NTFS filesystem cache and consequently
avoids the overhead of moving data between the FS cache, the .NET cache and
the application space, you are also loosing the advantage of the FS cache
when multiple applications might read the same file or if the application
updates the file data. Some applications, like file copy programs or
databases, might take advantage of un-buffered IO while most others don't
need or want this at all.

Willy.
 
G

Guest

Do you have a performance issue when you are using FileStream?

If the answer is yes, you should search for a faster method, which will
likely entail poking into the Windows API. This will cause a small perf hit
with Interop, but you will likely gain it back with API file manipulation.

If no, streams are very lightweight and fast. I cannot, at present, think of
a faster "all .NET" solution.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top