Speed of read vs. fread

Paul Jensen pdj at nbires.UUCP
Thu Jan 24 11:15:30 AEST 1985


> Which is faster, read/write or fread/fwrite?
> ...
> 	Bob Lied	ihnp4!ihlts!lied

We found that fread was MUCH slower than read on our Vax 780 
when reading structures greater than about 256 - 512 bytes, 
and MUCH faster than read below that. It is about a factor of 
3 slower than read for 8K chunks, and about 5 times slower than read
for 16 byte chunks.  fwrite behaves similarly.

The problem is apparently due to the per character
overhead of getc/putc.  Fortunately, there is a simple fix.
If you peek to see how much is currently in the buffer 
and use bcopy (or memcpy) to move in what you need,
you can get fread's performance back up to what read can do 
for large chunks and still have the big 5:1 advantage over read 
for small chunks.  Thus, you shouldn't have to choose.
-- 
Paul Jensen	NBI {hao,ucbvax,allegra}!nbires!pdj	(303) 444-5710 x3054



More information about the Comp.unix mailing list