Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucsd!ogccse!orstcs!prism!johng From: johng@prism.cs.orst.edu (John A. Gregor) Newsgroups: comp.lang.c Subject: Re: Test of possible ACSGATE on net Message-ID: <13130@orstcs.CS.ORST.EDU> Date: 16 Oct 89 03:09:44 GMT References: <16103@nswitgould.cs.uts.oz> Sender: usenet@orstcs.CS.ORST.EDU Reply-To: johng@prism.CS.ORST.EDU (John A. Gregor) Organization: Oregon State Univ. -- Computer Science Lines: 26 In article <16103@nswitgould.cs.uts.oz> garth_kidd%680.808@fidogate.fido.oz (Garth Kidd) writes: >Can anyone think of a tighter way of coding a filter to strip spaces >from the input stream than this? > >main() >{ > char ch; > > for(;read(0,&ch,1)==1;write(1,&ch,(ch==' ')?0:1)) > ; >} Ick! Yuk! First, there are no awards for 'tight' source (small does not mean efficient). The killer is the 1 character reads and writes. There is this little problem with system calls, it's known as overhead. For each system call, your machine will have to do 2 context switches which are pretty expensive. Although it is more programmer work, what you should do is create two large buffers (one for reading and one for writing). Or you can use fread and fwrite, or getc and putc, which are also buffered. Lastly, do you have something against whitespace??? John Gregor johng@cs.orst.edu