Optimal buffer size for fread/fwrite [closed]

Want to improve this question? Update the question so it's on-topic for Software Engineering Stack Exchange.

Closed 11 years ago . What buffer size should I choose for read/write files via POSIX fread/fwrite functions? asked Oct 25, 2012 at 7:00 user55777 user55777

This question is too localized. It only pertains to whatever particular program you are coding at the moment.

Commented Oct 25, 2012 at 7:04

I mean general case. Many programs needs to read/write whole files with very unknown size (like grep , cp )

Commented Oct 25, 2012 at 7:09 Do you mean POSIX read / write functions or ANSI/ISO/IEC fread / fwrite functions? Commented Oct 25, 2012 at 8:19

1 Answer 1

The ANSI/ISO fread/fwrite functions are buffered. The buffer is usually 8 KiB and that gives the granularity independent of what you use in your code. It might make sense to increase the buffer a bit, perhaps to the value below. For bulk transfer they will always be a tiny bit slower though due to the extra copies.

For the POSIX read/write functions it depends on operating system and device and a lot of other things, but practical experience is that you don't get any performance improvement by increasing the buffer beyond tens of KiB, so 32 or 64 KiB is about right.

On some systems the dependency is larger than on others. On Linux the difference is usually minimal above 8 KiB (so the default buffer is fine), e.g. on Windows CE (using native API; they don't have POSIX) even bigger than 64 KiB still help. It might also depend on the device.