Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
bufferedstream MSDN:Adds a buffering layer to read and write operations on another stream

User bufferedstream always faster than any stream?Just like network stream,filestream or others.
If not,when we should use bufferedstream?
Posted

Suppose you have an open file, and you write it byte-by-byte, one byte per one call.
Each write operation will write a byte to the physical disk, this will be very slow.
And if you use BufferedStream, it will write each byte into a temporary buffer stored in memory which will be significantly faster. When the buffer will be large (you specify its size in constructor of BufferedStream), a physical write will occur.

Just to compare:
without BufferedStream - you go to the shop and buy meat. Then you return home, go to the shop again and buy milk... etc...
with BufferedStream - you go to the shop and buy all at once: milk, meat and cheese. Then go home. :)
 
Share this answer
 
v2
Comments
Richard MacCutchan 20-May-10 16:00pm    
I like your analogy, it's always good to relate concepts to real world situations.
kevein2008 20-May-10 21:50pm    
Thank you!
As you say,does bufferedstream always faster than any stream if my buffer not be large?
That means we can use bufferedstream at anywhere.I thank that impossible,because there are so many stream,why exist?
Dimitri Witkowski 21-May-10 0:10am    
Yeah, it's faster, but sometimes it's not applicable.
> I thank that impossible,because there are so many stream,why exist?
Ohh, I didn't understood you. What do you mean?
Buffering is most use when you have a stream that reads or writes short blocks of data, the buffering reduces the number of actual disk accesses so improving overall performance. There is a good explanation of the system on this[^] MSDN page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900