Test of possible ACSGATE on net
John A. Gregor
johng at prism.cs.orst.edu
Mon Oct 16 13:09:44 AEST 1989
In article <16103 at nswitgould.cs.uts.oz> garth_kidd%680.808 at 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 at cs.orst.edu
More information about the Comp.lang.c
mailing list