[comp.unix.shell] read in /bin/sh appears to be VERY cpu costly.

ggm@brolga.cc.uq.oz.au (George Michaelson) (05/08/91)

I have written a very crude shellscript to do while loops over a read
command. This is to permit a user attached to a terminal that cannot
do control characters (!) to get access to unix and do some useful stuff
with MH and editors.

I have just found out she incurred around 300 seconds of CPU time on a
sun 4 in 1 login session.

simple tests using time on a shellscript of:

	while 1
	do
		read INP
		echo $INP
	done

seem to suggest that read is incredibly expensive on the CPU.

Is this true, and is there any "cheaper" way to do interactive input
from a terminal within shellscripts?

Is this a "feature" in SunOS 4.1.1 /bin/sh?

	-George
-- 
	G.Michaelson
Internet: G.Michaelson@cc.uq.oz.au                     Phone: +61 7 365 4079
  Postal: George Michaelson, the Prentice Centre,
          The University of Queensland, St Lucia, QLD Australia 4072. 

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) (05/11/91)

As quoted from <1991May8.013013.29811@brolga.cc.uq.oz.au> by ggm@brolga.cc.uq.oz.au (George Michaelson):
+---------------
| simple tests using time on a shellscript of:
| 
| 	while 1
| 	do
| 		read INP
| 		echo $INP
| 	done
| 
| seem to suggest that read is incredibly expensive on the CPU.
+---------------

Do you know that it's "read" that is expensive?  The other possibilities are:
"while/do/done" and "echo".  Not to mention that "while 1" returns "1: not
found" on my system (admittedly, not a Sun 4), and the cost of interpolating
and expanding the "echo" command line.

A better test of the speed of "read" is to time a script containing only
"read" statements.  Also time an empty script so you can factor out the shell
startup time, for still better accuracy.

(Note: if your timing tests did in fact take this into account, you should
probably have said so.)

++Brandon
-- 
Me: Brandon S. Allbery			  Ham: KB8JRR/AA  10m,6m,2m,220,440,1.2
Internet: allbery@NCoast.ORG		       (restricted HF at present)
Delphi: ALLBERY				 AMPR: kb8jrr.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery       KB8JRR @ WA8BXN.OH

heinz@cc.univie.ac.at (05/15/91)

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) writes:

>As quoted from <1991May8.013013.29811@brolga.cc.uq.oz.au> by ggm@brolga.cc.uq.oz.au (George Michaelson):
>+---------------
>| simple tests using time on a shellscript of:
>|
>| 	while 1
>| 	do
>| 		read INP
>| 		echo $INP
>| 	done
>|
>| seem to suggest that read is incredibly expensive on the CPU.
>+---------------

>Do you know that it's "read" that is expensive?  The other possibilities are:
>"while/do/done" and "echo".  Not to mention that "while 1" returns "1: not
>found" on my system (admittedly, not a Sun 4), and the cost of interpolating
>and expanding the "echo" command line.

Well, "read" shouldn't consume too much CPU time, since it is essentially a
blocking I/O operation and doesn't account to CPU consumption. What really *is*
costly is the while/do/done. I've tried a while-loop in the GNU-bash on a SUN-3,
where the loop body was a single "echo", and it put an awful load on the
machine (actually, by running this loop simultaneously in several windows, you
can prevent other people -and yourself- from doing anything useful :)
Even a completely empty while-loop was sufficient to pose a significant delay
on other jobs.
So it is the shell itself that consumes the CPU time.

--
Heinz Herbeck (heinz@sophie.pri.univie.ac.at)