Any point to use BufferedStream when we have internalbuffer

T

Tony Johansson

Hi!

I know about the dispose pattern so you don't have to remaind me about that.

In this small code snipped(marked Ex1) an internal buffer(exist by default)
is used to store the data and when we do a flush or when the internal buffer
is full the data is forced to to underlaying stream.
Now to my question is it any point to use BufferedStream(Ex2) when you write
to a FileStream.
I mean we always have an internal buffer by default and this internal buffer
is probably large enought most of the times ?

Ex1
FileStream fs = new FileStream("Test.txt",FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Close();
fs.Close();

Ex2
//Here an example of the BufferedStream
FileStream fileStream = new FileStream("Test.txt",FileMode.Create);
BufferedStream buffStream = new BufferedStream(fileStream );
StreamWriter sw = new StreamWriter(buffStream );
sw.Close();
buffStream.Close();
fileStream .Close();

//Tony
 
P

Peter Duniho

Tony said:
Hi!

I know about the dispose pattern so you don't have to remaind me about that.

In this small code snipped(marked Ex1) an internal buffer(exist by default)
is used to store the data and when we do a flush or when the internal buffer
is full the data is forced to to underlaying stream.
Now to my question is it any point to use BufferedStream(Ex2) when you write
to a FileStream.
I mean we always have an internal buffer by default and this internal buffer
is probably large enought most of the times ?

I think it's less about the size of the existing buffer and more about
your own code's pattern. That is, how much does _it_ read at a time.

That said, unfortunately the MSDN docs don't offer much specific
guidance, but I'll point out that BufferedStream is not required for
correct code operation. It's a performance optimization. As such, the
only reliable way to know whether it's needed is to actually try it,
measuring performance, and see if it helps.

I would expect in general for it not to help for FileStreams, because
the default buffering for FileStream works pretty well. But note that
even for a FileStream, your code may or may not have control over the
creation and buffering options of the FileStream. In those cases,
BufferedStream might still be useful.

In any case, the only way to know for sure is test and measure.

Pete
 
A

Arne Vajhøj

In this small code snipped(marked Ex1) an internal buffer(exist by default)
is used to store the data and when we do a flush or when the internal buffer
is full the data is forced to to underlaying stream.
Now to my question is it any point to use BufferedStream(Ex2) when you write
to a FileStream.
I mean we always have an internal buffer by default and this internal buffer
is probably large enought most of the times ?

Ex1
FileStream fs = new FileStream("Test.txt",FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Close();
fs.Close();

Ex2
//Here an example of the BufferedStream
FileStream fileStream = new FileStream("Test.txt",FileMode.Create);
BufferedStream buffStream = new BufferedStream(fileStream );
StreamWriter sw = new StreamWriter(buffStream );
sw.Close();
buffStream.Close();
fileStream .Close();

If I do not need to write a lot (read: buffering is not
critical for performance) then I would use Ex1.

It has a small buffer and will do OK.

The docs does not specify buffer size, but Reflector
reveals that it is 1024.

If I really needed a large buffer for performance then
I would use one of:

Ex2B

FileStream fileStream = new FileStream("Test.txt",FileMode.Create);
BufferedStream buffStream = new BufferedStream(fileStream, 20480);
StreamWriter sw = new StreamWriter(buffStream );
sw.Close();
buffStream.Close();
fileStream .Close();

Ex3

FileStream fs = new FileStream("Test.txt",FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8, 20480);
sw.Close();
fs.Close();

Ex2B instead of Ex2, because the default buffer size for
BufferedStream is just 4096, which is only 4 times of what
StreamWriter is really using.

But I would probably prefer Ex3, because there are no need
to have the BufferedStream object at all when the functionality exists
in StreamWriter.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
If I do not need to write a lot (read: buffering is not
critical for performance) then I would use Ex1.

It has a small buffer and will do OK.

The docs does not specify buffer size, but Reflector
reveals that it is 1024.

If I really needed a large buffer for performance then
I would use one of:

Ex2B

FileStream fileStream = new FileStream("Test.txt",FileMode.Create);
BufferedStream buffStream = new BufferedStream(fileStream, 20480);
StreamWriter sw = new StreamWriter(buffStream );
sw.Close();
buffStream.Close();
fileStream .Close();

Ex3

FileStream fs = new FileStream("Test.txt",FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8, 20480);
sw.Close();
fs.Close();

Ex2B instead of Ex2, because the default buffer size for
BufferedStream is just 4096, which is only 4 times of what
StreamWriter is really using.

But I would probably prefer Ex3, because there are no need
to have the BufferedStream object at all when the functionality exists
in StreamWriter.

Arne

Good comments Arne!!

//Tony
 

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