Subject: Re: Proposed rc.d changes....
To: Geoff Wing <mason@primenet.com.au>
From: Luke Mewburn <lukem@wasabisystems.com>
List: tech-userlevel
Date: 02/20/2001 15:13:27
On Tue, Feb 20, 2001 at 03:48:56AM +0000, Geoff Wing wrote:
> Luke Mewburn <lukem@wasabisystems.com> typed:
> :	- Other minor optimisations (use case instead of if to
> :	  compare strings, etc)
> 
> Why is that a bonus?  test/[ is builtin to /bin/sh these days (mainly
> for this reason - rc scripts) so there's no forking for it.

In a test harness I whipped up, case was faster than if.

For 50000 iterations of a simple test on my laptop, here's the time(1)
difference:
			case	if [	if /bin/test (1)
			----	----	----------------
	no match	0.44	0.96	344.0
	match, assign	0.64	1.13	355.0

    (1) done with 500 iterations and * 100; I couldn't be bothered waiting
	6 minutes for the test to run. :)

Sure, miniscule differences, but on slower machines, or systems where
test is not a builtin, this would be even more noticable.


The code in question was something like:

    case:
	a=${1:-foo}
	for i in `jot 50000`; do
		case "$a" in
		PID)
			result=match ;;
		esac
	done

    if [:
	a=${1:-foo}
	for i in `jot 50000`; do
		if [ "$a" = "PID" ]; then
			result=match
		fi
	done

    if /bin/test:
	a=${1:-foo}
	for i in `jot 50000`; do
		if /bin/test "$a" = "PID"; then
			result=match
		fi
	done