Demo: Buffering in User Space¶
The same ten million one-byte writes, once through the C library's buffer and once with the buffer switched off.
Run¶
cd ../../../../02-os-types-interface-full/demos/04-optimize-os-interface/fwrite
make
./fwrite_buffered
./fwrite_unbuffered
For where the time goes:
The only difference in the source is one setvbuf(..., _IONBF, ...).
The unbuffered version is more than twenty times slower.
Ask¶
- The output files are identical. Where did the twenty-fold difference go?
- With the buffer on, how many
writesystem calls does ten millionfwrites make? (strace -cwill tell you.) - What did the buffer cost, and why is it the default?