The BetterStreams Library

Home ] AsyncStream ] AlternateStreams ] [ BetterBufferedStream ] Performance ] Documentation ] Download ] Purchase ] Help ]

BetterBufferedStream

BetterBufferedStream is similar to the Framework's System.IO.BufferedStream but improves seeking while reading a stream.  BufferedStreams (and FileStreams) are extremely inefficient when repositioning within buffered data--instead of simply moving forward or backward within the buffer as BetterBufferedStream does, they will instead seek on the underlying the stream and then completely refill the buffer, yielding very poor performance.  BetterBufferedStream can also optionally "read behind" a specified number of bytes to ensure efficient peeking.  [example]

BetterBufferedStreams and BufferedStreams have approximately the same performance when writing.

Download an evaluation copy of the library to try it in your own applications (or use the included test package).  You can also view the documentation for this class online.

Performance

The following performance was observed when reading every other kilobyte (alternating between reading 1024 bytes and advancing the stream position by 1024) of a 4GB file [testing methodology]:

With a 100KB buffer BufferedStream and FileStream refill the buffer fifty times every 100KB, reading every byte from the underlying stream approximately fifty times (compared to once for AsyncStream and BetterBufferedStream); their performance is worse with larger buffers or more frequent seeks.  Because the disk performs its own caching the 100KB BufferedStream and FileStream do not actually take fifty times as long, but this mitigating factor not present when reading other types of streams.