[net.unix-wizards] Relative speed of Bourne vs. C Shells - C Shell is faster.

idallen@watmath.UUCP (03/24/85)

I'm surprised at the comments that the Bourne Shell is faster than the
C Shell.  The 4.2bsd Bourne Shell has to call a program to add two
numbers together, print a message, or perform an IF statment -- the C
Shell does all that using built-in code.  Waterloo has some large shell
scripts that would not be practical if written in Bourne format.

I don't like the C Shell bugs, but when I can work around them the C
Shell gives me much faster performance for my large shell scripts.
KSH recognized the cost of calling programs to do simple things (such
as add or compare numbers), and moved all that code into the shell
itself.   Perhaps other Bourne Shells have done this, but be sure that
your version does before you claim it is faster than the C Shell.

For comparison:
-------------------------------------------------------------------------
#!/bin/csh -f
@ x=1
while ( $x < 100 )
	@ x++
	if ( $x > 10 ) echo Hi! $x
end
-------------------------------------------------------------------------
#!/bin/sh
x=1
while [ $x -lt 100 ]; do
	x=`expr $x + 1`
	if [ $x -gt 10 ]; then
		echo Hi! $x
	fi
done
-------------------------------------------------------------------------
On our VAX/780 running 4.2bsd, the Bourne script uses 25 seconds user CPU
and 138 seconds system CPU.  The C Shell script uses 11 seconds user CPU
and 4 seconds system CPU.  The Bourne script has to fork four processes
for each loop iteration; the C Shell none.
-- 
        -IAN!  (Ian! D. Allen)      University of Waterloo

henry@utzoo.UUCP (Henry Spencer) (03/25/85)

> I'm surprised at the comments that the Bourne Shell is faster than the
> C Shell.  The 4.2bsd Bourne Shell has to call a program to add two
> numbers together, print a message, or perform an IF statment -- the C
> Shell does all that using built-in code.  Waterloo has some large shell
> scripts that would not be practical if written in Bourne format.

The discussion naturally refers to modern Bourne shells, which the 4.2
one is not.  This is one area where System V has actually made some
useful contributions.  We have *lots* of shell scripts that would not
be practical if run under an old Bourne shell (or, I suspect, under the
C shell).
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

tiberio@seismo.UUCP (Mike Tiberio) (03/26/85)

I too am sick and tired of hearing about how much faster the bourne shell
is than the C shell. On a heavily loaded VAX my C shell scripts always run
faster, and when the load drops to around 1 the two shells seem to run neck
and neck. Some people say bourne shell is easier to program, come on, does
"if test $# -gt 1" really look better than "if ($#argv > 1)" and tell me you
don't cringe every time you type esac and fi (yuk yuk). Here are two shell
scripts (made to make tail act like head) go ahead, run em, make my day..
---seismo!tiberio

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#! /bin/sh
case $1 in
	-*) O=$1;shift
esac
for F
do
	if test $# -gt 1
	then	echo "";echo ==\> $F \<==
	fi
	/usr/ucb/tail $O $F
done
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#! /bin/csh -f
set O
switch ($1)
	case -*:
		set O=$1;shift
endsw
foreach F ($*)
	if ($#argv > 1) then
		echo "";echo ==\> $F \<==
	endif
	/usr/ucb/tail $O $F
end
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

mike@whuxl.UUCP (BALDWIN) (03/28/85)

> I too am sick and tired of hearing about how much faster the bourne shell
> is than the C shell. On a heavily loaded VAX my C shell scripts always run
> faster, and when the load drops to around 1 the two shells seem to run neck
> and neck. Some people say bourne shell is easier to program, come on, does
> "if test $# -gt 1" really look better than "if ($#argv > 1)" and tell me you
> don't cringe every time you type esac and fi (yuk yuk). Here are two shell
> scripts (made to make tail act like head) go ahead, run em, make my day..
> ---seismo!tiberio

OK, I did run 'em.  Ten times, on twenty files each time.  The results
of the time command are:
								   total
sh:
real   3.3   2.9   2.9   2.9   2.9   2.9   2.9   2.9   2.9   3.0   29.5
user   0.7   0.8   0.8   0.7   0.7   0.7   0.7   0.7   0.7   0.8   7.3
sys    2.1   1.9   2.0   2.1   2.0   2.0   2.0   2.0   2.1   2.0   20.2

csh:
real   8.4   5.9   5.9   7.8   5.9   5.9   5.9   6.1   6.0   5.9   63.7
user   2.9   2.9   2.8   2.8   2.9   2.9   2.8   2.9   2.8   2.8   28.5
sys    3.0   2.8   2.9   2.9   2.9   2.9   2.9   2.9   2.9   3.0   29.1

Relative speed comparison:

      sh/csh

real  46.3%
user  25.6%
sys   69.4%

This is with the SVR2 sh, by the way.  I'm not sick and tired of people
making false claims (people always will), I'm just amused.

							Michael Baldwin
							whuxl!mike

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (03/28/85)

> On our VAX/780 running 4.2bsd, the Bourne script uses 25 seconds user CPU
> and 138 seconds system CPU.  The C Shell script uses 11 seconds user CPU
> and 4 seconds system CPU.  The Bourne script has to fork four processes
> for each loop iteration; the C Shell none.

??  I found the following:

	CSH script		SH script

	 7.2 user		 6.2 user
	 2.1 system		10.9 system

Of course, the extra "system" time is because you had the Bourne shell
execing "expr" in a loop, since it does not have arithmetic built in.
(The Korn shell does..)  In spite of this severely biased comparison,
the Bourne shell did not do badly; indeed the shell itself was quicker.

Our Cshell is essentially 4.2+BSD and the Bourne shell is SVR2 under
the BRL UNIX System V emulation.  Maybe you need a better Bourne shell?

mat@amdahl.UUCP (Mike Taylor) (03/28/85)

> I'm surprised at the comments that the Bourne Shell is faster than the
> C Shell.  The 4.2bsd Bourne Shell has to call a program to add two
> numbers together, print a message, or perform an IF statment -- the C
> Shell does all that using built-in code.  Waterloo has some large shell
> scripts that would not be practical if written in Bourne format.
> 
> I don't like the C Shell bugs, but when I can work around them the C
> Shell gives me much faster performance for my large shell scripts.
> KSH recognized the cost of calling programs to do simple things (such
> as add or compare numbers), and moved all that code into the shell
> itself.   Perhaps other Bourne Shells have done this, but be sure that
> your version does before you claim it is faster than the C Shell.
>        ---- shell scripts ----

To add some more results for the given shell scripts (in seconds),

Shell  -Amdahl 470 V/8-    ----Vax11/780---
         UTS/V (SVR2)           4.2 bsd
       User System Total   User System Total
csh    1.21  0.38   1.59    11     4    15
 sh    0.96  1.76   2.72    25   138   163
ksh    0.91  2.05   2.96    --    --    --

-- 
Mike Taylor                        ...!{ihnp4,hplabs,amd,sun}!amdahl!mat

[ This may not reflect my opinion, let alone anyone else's.  ]

toddb@tekcrl.UUCP (Todd Brunhoff) (03/29/85)

In article <12138@watmath.UUCP> idallen@watmath.UUCP (Ian! D. Allen) writes:
>I'm surprised at the comments that the Bourne Shell is faster than the
>C Shell.  The 4.2bsd Bourne Shell has to call a program to add two
>numbers together, print a message, or perform an IF statment -- the C
>Shell does all that using built-in code.  Waterloo has some large shell
>scripts that would not be practical if written in Bourne format.
>
>I don't like the C Shell bugs, but when I can work around them the C
>Shell gives me much faster performance for my large shell scripts.
>KSH recognized the cost of calling programs to do simple things (such
>as add or compare numbers), and moved all that code into the shell
>itself.   Perhaps other Bourne Shells have done this, but be sure that
>your version does before you claim it is faster than the C Shell.
>
>For comparison:
>-------------------------------------------------------------------------
>#!/bin/csh -f
>@ x=1
>while ( $x < 100 )
>	@ x++
>	if ( $x > 10 ) echo Hi! $x
>end
>-------------------------------------------------------------------------
>#!/bin/sh
>x=1
>while [ $x -lt 100 ]; do
>	x=`expr $x + 1`
>	if [ $x -gt 10 ]; then
>		echo Hi! $x
>	fi
>done
>-------------------------------------------------------------------------
>On our VAX/780 running 4.2bsd, the Bourne script uses 25 seconds user CPU
>and 138 seconds system CPU.  The C Shell script uses 11 seconds user CPU
>and 4 seconds system CPU.  The Bourne script has to fork four processes
>for each loop iteration; the C Shell none.
>-- 
>        -IAN!  (Ian! D. Allen)      University of Waterloo

I consider that a rather silly comparison since neither shell was designed
for numeric processing.  You wouldn't use Fortran or C for evaluating
predicates like you would find in Prolog, etc, etc.  A better solution for
your "application" (which uses about .8u and 1.2s) would be

#!/bin/sh
awk '
BEGIN {
	x = 1;
	while (x < 100)
		if (x++ > 10)
			print "Hi!" x
	exit
}'

Nevertheless, your point is well taken; the Bourne shell is slower for using
the loops that you used.  However, I should have to agree with the
"others" that claim the Bourne shell is faster when using the right
vernacular.  For instance, you should be using "case-esac" and "for" loops
instead of the [ commands.

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (03/29/85)

> ... Here are two shell
> scripts (made to make tail act like head) go ahead, run em, make my day..

I took the Tiberio challenge and the Bourne shell script was slightly
faster than the Cshell script, but not much.

There are really two good reasons for using the Bourne/Korn shell
for scripts:
	(1)  The shell is available on every post-Sixth Edition UNIX.
	(2)  It has a formal grammar.
The usual arguments:
	(3)  It is faster.
	(4)  It is more readable.
aren't nearly as important.

I think the real question is, why doesn't AT&T further upgrade the
Bourne shell a la Korn so that there is nothing that the Cshell has
left to offer.  The major functional differences so far have been:
	(a)  History.  Still not in Bourne shell but okay in Korn.
	(b)  Job control.  Added to Bourne shell at BRL, also in Korn.
	(c)  Aliases.  Pretty much remedied by SVR2 shell functions.
	(d)  Lots of speed hacks.  Now in Bourne shell also.

ignatz@aicchi.UUCP (Ihnat) (03/29/85)

Aw, c'mon, guys...this belongs in net.religion, along with arguments on
the best screen editor, the best operating system, and whether Szechwan
is hotter than Hunan...

	(ash, CP/M ed, MULTICS, and Thai...)

-- 
	Dave Ihnat
	Analysts International Corporation
	(312) 882-4673
	ihnp4!aicchi!ignatz

jerryp@tektools.UUCP (Jerry Peek) (03/29/85)

[...getting onto a tangent...]

In article <84@tekcrl.UUCP> toddb@tekcrl.UUCP (Todd Brunhoff) writes:
> 
> I consider that a rather silly comparison since neither shell was designed
> for numeric processing.  You wouldn't use Fortran or C for evaluating
> predicates like you would find in Prolog, etc, etc.  A better solution for
> your "application" (which uses about .8u and 1.2s) would be
> 
> #!/bin/sh
> awk '
> BEGIN {
> 	x = 1;
> 	while (x < 100)
> 		if (x++ > 10)
> 			print "Hi!" x
> 	exit
> }'

Though this wasn't the point Todd was trying to make, it's a good place to
use the magic-number business to feed the script *directly* to awk, and never
start a shell *at all*.  The following setup shaves the script above
(about 0.4u and 0.4s) down to 0.3u and 0.2s:

	#! /bin/awk -f
	BEGIN {
		x = 1;
		while (x < 100)
			if (x++ > 10)
				print "Hi!" x
		exit
	}


--Jerry Peek, UNIX Training Instructor, Tektronix, Inc.
US Mail:    MS 76-036, P.O. Box 500, Beaverton, OR 97077
uucp:       {allegra,decvax,hplabs,ihnp4,ucbvax}!tektronix!tektools!jerryp
CS,ARPAnet: jerryp%tektools@tektronix.csnet
Phone:      503/627-1603

toby@gargoyle.UChicago.UUCP (Toby Harness) (04/02/85)

In article <> gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) writes:
>There are really two good reasons for using the Bourne/Korn shell
>for scripts:
>	(1)  The shell is available on every post-Sixth Edition UNIX.
>	(2)  It has a formal grammar.
>The usual arguments:
>	(3)  It is faster.
>	(4)  It is more readable.
>aren't nearly as important.

Doug makes a good point:  everyone has the Bourne shell.  In general,
people should write all their scripts in it.  Why then does every other
shell need to duplicate what it provides?

What ever happened to the notion of 'tools'?  Why does a command interpreter
have to be an interactive programming language?

Like many people reading this, I use (t)csh as my command interpreter, but
write shell scripts in the Bourne shell.  Except for a few things like
'while 1' or 'foreach', I don`t use many of csh`s programming constructs.
If I need to do something fancy, I first type 'sh'.  On the other hand,
I am always using history, job control (when available), and aliases.

Instead, what about a shell that has extensive file, command, and
directory name features (tcsh`s command completion is a good example), but
little programming ability.  It could be coupled with a variety of
script command languages (that of course could read from stdin) with
different styles of syntax:  sh is algol-like; how about lisp (imagine
programming a shell with mock-lisp), or prolog, or whatever you like.


Toby Harness		Ogburn/Stouffer Center, University of Chicago
			...ihnp4!gargoyle!toby

jsdy@hadron.UUCP (Joseph S. D. Yao) (04/11/85)

> [...getting onto a tangent...]
>                     ...  The following setup shaves the script above
> (about 0.4u and 0.4s) down to 0.3u and 0.2s:
> 	#! /bin/awk -f
> 	BEGIN {
> 		x = 1;
> 		while (x < 100)
> 			if (x++ > 10)
> 				print "Hi!" x
> 		exit
> 	}

Near as I can tell, the first line only works if you are running the
C shell under 4BSD, which interprets the #! business.

	Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}

thomas@utah-gr.UUCP (Spencer W. Thomas) (04/15/85)

In article <162@hadron.UUCP> jsdy@hadron.UUCP (Joseph S. D. Yao) writes:
>Near as I can tell, the first line only works if you are running the
>C shell under 4BSD, which interprets the #! business.

This is a common misconception, that has been "cleared up" many times on
this newsgroup.  It is NOT THE CSH that interperts the #! business, but
the kernel.  (I have also heard claims that execvp does it.  This is not
true either.)  Again: the KERNEL interprets the #! "magic number", NOT
THE SHELL.

(Will this be the last time? (Of course not!))
-- 
=Spencer   ({ihnp4,decvax}!utah-cs!thomas, thomas@utah-cs.ARPA)
	"Controversy equalizes fools and wise men in the same way
	 -- *and the fools know it*." -- Oliver Wendell Holmes

ed@mtxinu.UUCP (Ed Gould) (04/17/85)

> > 	#! /bin/awk -f
> 
> Near as I can tell, the first line only works if you are running the
> C shell under 4BSD, which interprets the #! business.

On 4.1BSD and later systems, #! is a magic number that the kernel
exec routine recognizes.  The format of the line is

    #!<space-or-tab><interpreter-full-path-name><space-or-tab><argument>\n

Only one argument may be supplied.  Any interpreter may be specified;
the above example uses /bin/awk rather than the more common /bin/sh
and /bin/csh.

-- 
Ed Gould		    mt Xinu, 739 Allston Way, Berkeley, CA  94710  USA
{ucbvax,decvax}!mtxinu!ed   +1 415 644 0146

jsdy@hadron.UUCP (Joseph S. D. Yao) (04/19/85)

> > [...getting onto a tangent...]
> > 	#! /bin/awk -f
> Near as I can tell, the first line only works if you are running the
> C shell under 4BSD, which interprets the #! business.

AWK!!!  I can only plead late-night temporary brain damage.  This should
read:
... C shell  OR  4BSD
since, of course, this is done by the Kernel under 4BSD, and is
interpreted by the C shell under all other versions of {U,Q,X,Z,V,T}NIX.

;-,	(well, i was half-right?)

	Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}