You can improve read speed with BufferedStream, for example:
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (BufferedStream bs = new BufferedStream(fs)) using (StreamReader sr = new StreamReader(bs)) { string line; while ((line = sr.ReadLine()) != null) { } }
March 2013 UPDATE
I recently wrote code to read and process (search for text) 1 GB-ish text files (much larger than the files used here) and achieved a significant performance boost using the producer / consumer pattern. The manufacturer’s task is read in lines of text using a BufferedStream and transferred to a separate consumer task that performed the search.
I used this as an opportunity to learn the TPL Dataflow, which is very well suited for quickly coding this template.
Why BufferedStream is faster
A buffer is a block of bytes in memory used to cache data, thereby reducing the number of calls in the operating system. Buffers improve read and write performance. The buffer can be used to read or write, but never at the same time. BufferedStream's read and write methods automatically support a buffer.
December 2014 UPDATE: Your mileage may vary.
Based on the comments, FileStream should use BufferedStream internally. At the time this answer was first provided, I measured a significant performance improvement by adding BufferedStream. At that time I was guided by .NET 3.x on a 32-bit platform. Today, focusing on .NET 4.5 on a 64-bit platform, I do not see any improvements.
Similar
I came across a situation where the streams of a large, generated CSV file into the Response stream from an ASP.Net MVC action were very slow. Adding a BufferedStream in this case improved performance by 100x. See Unbuffered output very slowly for more details.
Eric J. Mar 10 '12 at 1:22 2012-03-10 01:22
source share