如何根据当前服务器找到最合适的缓冲区大小来读取或写入Stream [英] how to find the best suitable buffer size to read or write Stream based on the current server

查看:27
本文介绍了如何根据当前服务器找到最合适的缓冲区大小来读取或写入Stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个服务器,该服务器将准备就绪并写入巨大的文件/数据库.

I am writing a server which will ready and write huge files / database.

我在许多地方使用8192作为缓冲区大小的地方都使用了Stream读写功能.

I have used Stream read and write functions many places where I am using 8192 as buffer size.

我也在从TCP套接字读取大量输入.

I am also reading large input from TCP sockets.

我不知道将在其中部署服务的VM的配置是什么.

I don't know what would be the configuration of the VMs where the service will be deployed.

是否有任何内置函数可以用来确定最适合我的服务器的缓冲区大小?

Is there any built in function using which I can determine the best suitable buffer size for my server?

推荐答案

我自己经常想知道这一点.但是最后我不认为有一条通用的规则可以适用.它始终取决于您的特定需求.

I often wondered this myself. But in the end I do not hink that there is a general rule to apply. It always comes down to your specific needs.

根据经验,如果缓冲区较大,则到文件系统或数据库的往返次数较少,这通常是大多数情况下的最佳选择.

As a rule of thumb, if your buffer is bigger you need less roundtrips to the file system or database, which in general, is best for most cases.

但是,在不使用其他应用程序的情况下,系统一次可以将多少数据读入内存,这在很大程度上取决于您的个人环境.某些移动设备的特性可能与您的顶级服务器硬件不同,依此类推.

However, how much data your system can read into memory at once, without making other applications, is very depending on your individual environment. Some mobile device might have different specifics than your over-the-top server hardware, and so on.

要考虑的其他因素包括网络带宽和其他共享资源,以及对您的操作产生的绝对性能影响.

Other things to consider would be network bandwith and other shared resources, as well as the sheer performance impact on your actions.

例如,在一个具有数千个图像文件的项目中,我们经过几次尝试后发现-对我们而言-idela缓冲区大小约为1 MB.对于尺寸小于此尺寸的图像,我们使用的缓冲区大小等于文件大小.对于您的情况,这当然不适合.

For example, at a project with thousands of image files, we found after several tries that - for us - the idela buffer size was at around 1 MB. For images with a size lower than that we used a buffer size equal to the file size. For your scenario this would of course not fit.

Microsoft的性能专家Rico Mariani指出了性能编程的10个最重要的方面:度量,度量,度量,度量...(您明白了这一点:-))

Rico Mariani, performance expert at Microsoft, names the 10 most important aspects of programming for performance: Measure, measure, measure, measure, ... (You get the point. :-) )

这篇关于如何根据当前服务器找到最合适的缓冲区大小来读取或写入Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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