[comp.lang.c] Test of possible ACSGATE on net

garth_kidd%680.808@fidogate.fido.oz (Garth Kidd) (10/09/89)

Original to: csgtkidd@medusa.ua.oz
This is a test message, but while I'm at it, just a small question:
 
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))
    ;
}
 
Thankew for any help...
 
 
gk 


---
 * Origin: Australian BBS Registry (Opus 3:680/808)

cpcahil@virtech.UUCP (Conor P. Cahill) (10/16/89)

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))
>     ;
> }


If by "tighter" you mean better performing the following would 
outdo yours by probably an order of magnitude:

	#include <stdio.h>

	main()
	{
		int ch;

		while( (ch=getchar()) != EOF)
		{
			if( ch != ' ' )
			{
				putchar(ch);
			}
		}
	}

Actually, my "order of magnitude" estimation is incorrect.  Processing
90K of data using your program took 53 seconds of cpu time, while the
example I gave took .4 seconds (more like 2 orders of magnitude).
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

johng@prism.cs.orst.edu (John A. Gregor) (10/16/89)

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

gwyn@smoke.BRL.MIL (Doug Gwyn) (10/16/89)

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?

Actually, yes, but I don't want to encourage this utterly HORRIBLE program.
Did you realize that unbuffered 1-byte reads/writes impose tremendous
overhead?  Or that many systems do not have UNIX-compatible read()/write()
functions?

kremer@cs.odu.edu (Lloyd Kremer) (10/16/89)

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))
>    ;
>}

Others have mentioned the incredible overhead of 1-byte reads and writes,
but there is another problem with this.  On many systems, a write() of
zero bytes sends EOF.  The reading process may decide to exit after your
first write() of zero bytes.  The result would be to copy the input up to
but not including the first space.

-- 
					Lloyd Kremer
					...!uunet!xanth!kremer
					Have terminal...will hack!

diamond@csl.sony.co.jp (Norman Diamond) (10/17/89)

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))
>    ;
>}

Yeah!  Strip the spaces from it!!!!!!

(Oh by the way, a zillion calls to "getc" are faster than a zillion
1-byte calls to "read", and the same for half-a-zillion calls to "putc"
although you'll have to move your (ch==' ')?0:xxxx test around in front
of the "putc.  But this doesn't make it tighter.)

-- 
Norman Diamond, Sony Corp. (diamond%ws.sony.junet@uunet.uu.net seems to work)
  Should the preceding opinions be caught or     |  James Bond asked his
  killed, the sender will disavow all knowledge  |  ATT rep for a source
  of their activities or whereabouts.            |  licence to "kill".

karl@haddock.ima.isc.com (Karl Heuer) (10/17/89)

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

If tightness is of more concern than portabililty off UNIX, you could use
	main(){system("tr -d ' '");}
which can be further improved by making it a shell script.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

dg@lakart.UUCP (David Goodenough) (10/17/89)

garth_kidd%680.808@fidogate.fido.oz (Garth Kidd) sez:
>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))
>    ;
>}

#include <stdio.h>
main()
 {
    int ch;
    while ((ch = getchar()) != EOF)
      if (ch != ' ')
	putchar(ch);
 }

However, contemplate what will happen to the correctness of either of
the above programs if passed through such a filter. They will _BOTH_
break, either failing to compile or generating incorrect code if they
do compile.
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com			  +---+

andre@targon.UUCP (andre) (10/18/89)

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))
>    ;
>}

I'm going to be flamed for this but...

main(c)
{
  while(read(0,&c,1)>0&&(c-' '?write(1,&c,1):1))
    ;
}

-- 
The mail|    AAA         DDDD  It's not the kill, but the thrill of the chase.
demon...|   AA AAvv   vvDD  DD        Ketchup is a vegetable.
hits!.@&|  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
--more--| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre

condict@cs.vu.nl (Michael Condict) (10/19/89)

In article <647@targon.UUCP> andre@targon.UUCP (andre) writes:
|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?
|    [ Horribly inefficient, unreadable program omitted ]
|I'm going to be flamed for this but...
|
|main(c)
|{
|  while(read(0,&c,1)>0&&(c-' '?write(1,&c,1):1))
|    ;
|}

And well you should be.  All other considerations aside, your version
won't work on machines where &c is the address of the most significant
byte of c, rather than the least significant byte.  On such machines
you read each character into the most significant byte of c, then
(c-' ') compares the least significant byte to ' '.

Michael Condict
Vrije University
Amsterdam