简体   繁体   English

如何估计从NetworkStream读取的字节数组/缓冲区的大小?

[英]How to estimate the size of byte array/buffer to read from NetworkStream?

int bufferSize = 8192;
Byte[] buffer = new Byte[bufferSize]; 

I need to read from a NetworkStream which would have a continuous flow of incoming data at a high rate. 我需要从NetworkStream中读取,这将以高速率连续输入数据流。 I wanted to know 我想知道

  • What should be the basis for setting the bufferSize value? 什么应该是设置bufferSize值的基础?
  • Is there a standard "safe" value that can be chosen irrespective of the enviornment? 是否存在可以选择的标准“安全”值,而与环境无关?
  • Should it also vary with the Stream I'm trying to read from (FileStream/NetworkStream/..)? 它是否也会随着我想要读取的Stream(FileStream / NetworkStream / ..)而变化?

Please consider performance as the focus point for the estimation. 请将绩效视为估算的重点。

There is only one sure way to optimize performance: measure, measure, measure. 优化绩效只有一种可靠的方法:衡量,衡量和衡量。

I've seen 1024, 4096 or 8192 used as buffer sizes for downloading files from the internet, and I haven't noticed any significant difference using either one. 我已经看到1024,4096或8192用作从Internet上下载文件的缓冲区大小,我没有注意到使用任何一个有任何显着差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM