Speed of read vs. fread

Guy Harris guy at rlgvax.UUCP
Sat Jan 26 15:15:40 AEST 1985


> Which is faster, read/write or fread/fwrite?
> 
> I've been told that fread/fwrite is faster because
> it buffers.  On the other hand, I've been told that
> read/write is faster because (a) read always does
> a one block read-ahead if it can and (b) it avoids
> the overhead of the fread/fwrite abstraction.

a) is prunejuice; "fread" calls "read", so you get the same
read-ahead.  b) is true as long as your "read"s are the same
size as the ones that "fread" does.  The overhead of the
abstraction is MUCH lower in System V Release 2 and will
be lower in 4.3BSD (originally, "fread" was a loop containing
a "getc" macro"; it has been redone to move entire blocks of
characters from the buffer to the user directly).  Still, this
overhead is non-zero.

The point about buffering is that doing "fread" to read 16 bytes
at a time will probably be better than doing "read" to read
16 bytes.  The latter requires 64 times as many "read" calls (assuming
1024-byte standard I/O buffers), and system calls are expensive.

On 4.2BSD, the win of "fread" is even higher; the buffer size is the
same as the filesystem block size, which is usually 4096 or 8192 bytes.

	Guy Harris
	{seismo,ihnp4,allegra}!



More information about the Comp.unix mailing list