This example illustrates the effect of double-buffering in the standard I/O library on an x86 PIII-725 machine with a UDMA-4 EIDE disk, using the QNX 4 filesystem.
The table shows the rate, in megabytes per second, of writing and reading a 256 MB file, with a record size of 8 KB:
Scenario | Writing | Reading |
---|---|---|
File descriptor | 18 | 31 |
Standard I/O | 13 | 16 |
setvbuf() | 17 | 30 |
Here, you can see the effect of the default standard I/O buffer size (BUFSIZ, or 1 KB). When you ask it to transfer 8 KB, the library implements the transfer as 8 separate 1 KB operations. Note how the standard I/O case does match the above benchmark (see "Record size," above) for a 1 KB record, and the file-descriptor case is the same as the 8 KB scenario).
When you use setvbuf() or the STDIO_DEFAULT_BUFSIZE environment variable to force the standard I/O buffering up to the 8 KB record size, then the results come closer to the optimal file-descriptor case (the small difference is due to the extra code complexity and the additional memcpy() between the user data and the internal standard I/O FILE buffer).