Read File Fast

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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?
 
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.
 
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.
 
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.
 
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!
***************************
 
Back
Top