What buffer size should I choose for read/write files via POSIX fread/fwrite functions?
3
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.
2