It is currently Tue, 30 Nov 2021 17:56:20 GMT



 
Author Message
 Light Speed Bourne Shell! (was: Bourne shell tricks)
                              WARP SPEED TESTS!

For a light-speed bourne shell script, I just found out a really cool trick:
        if [ "$var" = "condition" ]; then list; fi
becomes:
        case "$var" in "condition") list;; esac

Take this example, two scripts:
        if.sh:
                for x in `jot 1000`; do
                        if [ $x = 1000 ]; then echo "Done"; fi
                        done
        case.sh:
                for x in `jot 1000`; do
                        case $x in 1000) echo "Done";; esac
                        done

("jot" is just some weird util we have here that prints out numbers)
Now take a look at the runtimes (on a DECstation/240 with not much load):
#0 /tmp/JAMtmp> time sh case.sh
                real    0m0.53s
                user    0m0.11s
                sys     0m0.05s
#0 /tmp/JAMtmp> time sh if.sh  
 ^--return code of last cmd in prompt ;-)
                real    0m32.43s
                user    0m1.95s
                sys     0m16.80s
So, case instead of test is:
real:   61 times faster
user:   18 times faster
sys :   31 times faster

...So use case instead of if whenever you can! (just remember the case will
interpret your condition as a pattern!)

--
     thur  Mail Address: LordArt...@vt.edu or jmaxw...@vt.edu
  n  r    
  a JAMax  "Thou shalt not kill...  Thou shalt not steal...
  h o   w   Thou shalt not covet thy neighbour's...ass nor any
  tan lle   thing that is thy neighbour's."  --Bible



 Tue, 14 Oct 1997 03:00:00 GMT   
 Light Speed Bourne Shell! (was: Bourne shell tricks)
jmaxw...@cray-ymp.acm.stuorg.vt.edu (Jon A. Maxwell) writes:
...

I completely agree that "case" is better than "test" for string comparisons,
however only other Ultrix users will get these numbers.  Ultrix has an older
Bourne shell with an external "test", so each test requires a vfork+exec.
So far as I know, all of the UNIX platforms available these days have test
built in with their Bourne shell, so its performance will be comparable
with case.

So for most the main advantage is the "case" syntax, which is more powerful
and flexible for matching strings.

        Donn Cave, University Computing Services, University of Washington
        d...@cac.washington.edu



 Tue, 14 Oct 1997 03:00:00 GMT   
 Light Speed Bourne Shell! (was: Bourne shell tricks)
Jon A. Maxwell (jmaxw...@cray-ymp.acm.stuorg.vt.edu) wrote:
:                               WARP SPEED TESTS!

: For a light-speed bourne shell script, I just found out a really cool trick:
:         if [ "$var" = "condition" ]; then list; fi
: becomes:
:         case "$var" in "condition") list;; esac

: Take this example, two scripts:
:         if.sh:
:                 for x in `jot 1000`; do
:                         if [ $x = 1000 ]; then echo "Done"; fi
:                         done
:         case.sh:
:                 for x in `jot 1000`; do
:                         case $x in 1000) echo "Done";; esac
:                         done

: ("jot" is just some weird util we have here that prints out numbers)
: Now take a look at the runtimes (on a DECstation/240 with not much load):
: #0 /tmp/JAMtmp> time sh case.sh
:                 real    0m0.53s
:                 user    0m0.11s
:                 sys     0m0.05s
: #0 /tmp/JAMtmp> time sh if.sh  
:  ^--return code of last cmd in prompt ;-)
:                 real    0m32.43s
:                 user    0m1.95s
:                 sys     0m16.80s
: So, case instead of test is:
: real:   61 times faster
: user:   18 times faster
: sys :   31 times faster

: ...So use case instead of if whenever you can! (just remember the case will
: interpret your condition as a pattern!)

: --
:      thur  Mail Address: LordArt...@vt.edu or jmaxw...@vt.edu
:   n  r    
:   a JAMax  "Thou shalt not kill...  Thou shalt not steal...
:   h o   w   Thou shalt not covet thy neighbour's...ass nor any
:   tan lle   thing that is thy neighbour's."  --Bible

Do me a favor.  Do a "type test" and tell me whether it's a shell
built-in.  On my system,  if.sh takes 150% of the time case does (and
almost twice as long if you use -eq) but my guess is you're loading test
each time.  Indeed,  when I substituted /bin/test for test,  our
figures coincided.

--
Dan Mercer                                            ClearSystems, Inc
Reply To:  damer...@mmm.com                           "The Mad Pedant"
======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients,  in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied.  Follow at your own risk.
Objects in the mirror are closer than they appear.



 Tue, 14 Oct 1997 03:00:00 GMT   
 Light Speed Bourne Shell! (was: Bourne shell tricks)
Yes, on some machines case is faster - pattern matching (globbing) is
a builtin, where '[' actually incurs the overhead of starting another
process - namely 'test'.  I always prefer 'case' over 'if' primarily
because it looks better and it expands well when you need to add more
cases.

...Don...

: Jon A. Maxwell (jmaxw...@cray-ymp.acm.stuorg.vt.edu) wrote:
: :                               WARP SPEED TESTS!

: : For a light-speed bourne shell script, I just found out a really cool trick:
: :         if [ "$var" = "condition" ]; then list; fi
: : becomes:
: :         case "$var" in "condition") list;; esac

: : Take this example, two scripts:
: :         if.sh:
: :                 for x in `jot 1000`; do
: :                         if [ $x = 1000 ]; then echo "Done"; fi
: :                         done
: :         case.sh:
: :                 for x in `jot 1000`; do
: :                         case $x in 1000) echo "Done";; esac
: :                         done

: : ("jot" is just some weird util we have here that prints out numbers)
: : Now take a look at the runtimes (on a DECstation/240 with not much load):
: : #0 /tmp/JAMtmp> time sh case.sh
: :                 real    0m0.53s
: :                 user    0m0.11s
: :                 sys     0m0.05s
: : #0 /tmp/JAMtmp> time sh if.sh  
: :  ^--return code of last cmd in prompt ;-)
: :                 real    0m32.43s
: :                 user    0m1.95s
: :                 sys     0m16.80s
: : So, case instead of test is:
: : real:   61 times faster
: : user:   18 times faster
: : sys :   31 times faster

: : ...So use case instead of if whenever you can! (just remember the case will
: : interpret your condition as a pattern!)

: : --
: :      thur  Mail Address: LordArt...@vt.edu or jmaxw...@vt.edu
: :   n  r    
: :   a JAMax  "Thou shalt not kill...  Thou shalt not steal...
: :   h o   w   Thou shalt not covet thy neighbour's...ass nor any
: :   tan lle   thing that is thy neighbour's."  --Bible

: Do me a favor.  Do a "type test" and tell me whether it's a shell
: built-in.  On my system,  if.sh takes 150% of the time case does (and
: almost twice as long if you use -eq) but my guess is you're loading test
: each time.  Indeed,  when I substituted /bin/test for test,  our
: figures coincided.

: --
: Dan Mercer                                            ClearSystems, Inc
: Reply To:  damer...@mmm.com                           "The Mad Pedant"
: ======================================================================
: All opinions expressed are my own and do not reflect the opinions of
: my employer or my employer's clients,  in particular 3M Company.
: All advice or software offered or presented is provided As Is with no
: warranty either expressed or implied.  Follow at your own risk.
: Objects in the mirror are closer than they appear.



 Sat, 18 Oct 1997 03:00:00 GMT   
 
   [ 4 post ] 

Similar Threads

1. Bourne Shell compatible shells (was: Request: which shells)

2. Bourne Shell EXPORT - Minor frustration at 2 am

3. getting environment variables from c shell using Bourne shell

4. Changing from the Bourne shell to C-shell

5. Changing from the Bourne shell to C-shell

6. Bourne Shell vs C-Shell

7. Process history under C-shell and Bourne shell

8. Bourne shell script to determine users shell

9. Bourne shell script affecting current shell with arguments


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software