In windows lets say i’m using the recv function to receive data from a socket.
I’m curious how big would an optimal buffer be? I could make it 1024 bytes or I could make it 51200 bytes, or bigger. I’m wondering which one would be better for performance.
This doesn’t only apply to the recv function, lets say im reading a large text file, do i want a very large buffer, or a smaller buffer?
THe operating system performs its own buffering, so the size of your buffer does not really matter. the performance penalty lies in the function call: a 1 byte buffer will be inefficient because it will require too much calls to
recv(). a buffer too big is just a waste of space.an optimal size will be something like twice the size of the data you expect to receive or are able to process in a single
recv()call, with a lower limit of approximately 1 or 2 tcp frame.i personally use a 4KB buffer, but that’s my own preference, and it depends largely on the application i am writing.