缓冲流使用 [英] bufferedstream use

查看:102
本文介绍了缓冲流使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bufferedstream MSDN:添加了一个缓冲层以对另一个流进行读写操作

用户缓冲流总是比任何流都快?就像网络流,文件流或其他流一样.
如果不是,什么时候应该使用bufferedstream?

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?

推荐答案

假设您有一个打开的文件,并按字节逐个写入,每个调用一个字节.
每次写操作都会向物理磁盘写入一个字节,这将非常慢.
而且,如果您使用BufferedStream,它将把每个字节写入存储在内存中的临时缓冲区中,这将大大加快速度.当缓冲区很大时(您在BufferedStream的构造函数中指定其大小),将发生物理写入.

只是比较一下:
没有BufferedStream-您去商店买肉.然后你回到家,再次去商店买牛奶...等等...
使用BufferedStream-您可以去商店立即购买所有产品:牛奶,肉和奶酪.那回去吧:)
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. :)


当您具有读取或写入短数据块的流时,缓冲是最有用的,缓冲会减少实际磁盘访问的次数,从而提高整体性能.在 [
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.


这篇关于缓冲流使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆