c# - 使用缓冲流与使用源流上的读取方法读取相同数量的字节有何不同? [英] c# - how using buffered stream is different than reading same number of bytes using read method on source stream?

查看:18
本文介绍了c# - 使用缓冲流与使用源流上的读取方法读取相同数量的字节有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用谷歌搜索并阅读了许多相关文章,但我并没有真正理解与使用流上的 read 方法一次读取相同数量的字节相比,使用 bufferstream 的性能如何更好.据我所知,bufferstream 一次从源流中读取许多字节以节省往返行程.但这与使用 read(byte[], index, count) 有什么不同,其中 count = 使用缓冲区流读取的字节数相同.请帮助我理解它.

googled for it and read many related articles but I didn't really understand how using bufferstream performs better when compared to reading same number of bytes at a time using read method on streams. from what I understand, bufferstream reads many number of bytes at a time from the source stream to save round trips. but how is this different than using read(byte[], index, count) where count = same number of bytes you read using buffer stream. Please help me understand it.

推荐答案

从多个数据源(例如文件)读取时,操作中最慢的部分通常是执行每个单独的读取请求.所以关键是减少说从源XXX给我N个字节的数据"的请求数量.

When reading from many data sources (such as files), the slowest part of the operation is often the execution of each individual read request. So the key is to reduce the number of requests saying 'give me N bytes of data from source XXX'.

在文件访问的情况下,10 MB 的 1 次读取很可能比 1 MB 的 10 次读取更有效率.

In the case of file access, it's very likely to be more efficient to do 1 read of 10 MB than it is to do 10 reads of 1 MB.

缓冲在幕后的作用是阅读比您要求的多,预计您可能会在不久之后再次阅读.

What buffering does under the hood is read more than you ask for, anticipating that you might read again shortly afterwards.

因此,虽然您只取回了您要求的数据块,但其余数据仍存储在缓冲区中.这样,当您下次请求数据时,您想要的数据通常已经在缓冲区中,对底层数据源的请求就更少了.

So, although you only get back the block of data you asked for, the rest is kept stored in the buffer. That way, when you next request data, the data you want is often already in the buffer, and fewer requests to the underlying data source are needed.

请注意,预读行为通常首先在创建缓冲区时执行,或者在执行第一个读取请求时执行.然后缓冲区将根据需要通过额外的预读来填充自己.

Note that the read-ahead behaviour is typically first performed at the creation of the buffer, or when the first read request is executed. Then the buffer will top itself up with additional read-aheads as required.

这篇关于c# - 使用缓冲流与使用源流上的读取方法读取相同数量的字节有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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