Relative speed of Bourne vs. C Shells - C Shell is faster.

Todd Brunhoff toddb at tekcrl.UUCP
Fri Mar 29 07:07:15 AEST 1985


In article <12138 at watmath.UUCP> idallen at 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.



More information about the Comp.unix.wizards mailing list