Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site rlgvax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!bellcore!decvax!genrad!panda!talcott!harvard!seismo!rlgvax!guy From: guy@rlgvax.UUCP (Guy Harris) Newsgroups: net.unix Subject: Re: Speed of read vs. fread Message-ID: <388@rlgvax.UUCP> Date: Sat, 26-Jan-85 00:15:40 EST Article-I.D.: rlgvax.388 Posted: Sat Jan 26 00:15:40 1985 Date-Received: Wed, 30-Jan-85 05:16:10 EST References: <626@ihlts.UUCP> Organization: CCI Office Systems Group, Reston, VA Lines: 27 > 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}!