Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!ulysses!dgk From: dgk@ulysses.homer.nj.att.com (David Korn[drew]) Newsgroups: comp.unix.questions Subject: Re: Some csh how-to, please Summary: while read; do ...; done performance Keywords: csh C-shell shell programming unix read Message-ID: <11407@ulysses.homer.nj.att.com> Date: 5 Apr 89 02:03:27 GMT References: <2127@pikes.Colorado.EDU> <7467@thorin.cs.unc.edu> <718@salgado.stan.UUCP> Organization: AT&T Bell Laboratories, Murray Hill Lines: 39 In article <718@salgado.stan.UUCP>, dce@stan.UUCP (David Elliott) writes: > In article <4258@omepd.UUCP> merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) writes: > >Why do people use 'cat' at every opportunity? Maybe that's why > >they sell so many multiprocessor machines? :-) :-) > > >Simpler: > > > > while read fie > > do > > something with $fie > > done > > >no additional processes. > > > In practice, you are unfortunately wrong. The best that redirection > gives you is one less exec. Current "standard" versions of sh (in > other words, most sh's other than ksh) fork when redirection is done > on a loop. > The big problem with cat file | while read ... is that it runs about 1/10th the speed of the redirection loop. The reason is that the shell (sh and/or ksh) have to read one character at a time when reading from a pipe. The reason for reading one character at a time is to make scripts like cat file | ( read line;cat) behead only a single line. Unfortunately there is not way to seek back on a pipe. Nor is there an ioctl() to specify line buffering. Thus, single characters reads are used. This is not the case when you specify redirection. I hope that someone will add a stream discipline that will cause pipes to use a line buffering protocol. Until such time, scripts would do better to use a temporary file rather than piping to a while read loop.