[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Command substitution

From: Chris F.A. Johnson
Subject: Command substitution
Date: Tue, 8 Apr 2003 10:49:20 -0400 (EDT)

Configuration Information [Automatically generated, do not change]:
Machine: i586
OS: linux-gnu
Compiler: i586-mandrake-linux-gnu-gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i586' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i586-mandrake-linux-gnu' 
-DCONF_VENDOR='mandrake' -DSHELL -DHAVE_CONFIG_H  -I.  -I. -I./include -I./lib 
-D_GNU_SOURCE  -O3 -fomit-frame-pointer -pipe -mcpu=pentiumpro -march=i586 
-ffast-math -fno-strength-reduce
uname output: Linux xword.rogers.com 2.4.19-16mdk #1 Fri Sep 20 18:15:05 CEST 
2002 i686 unknown unknown GNU/Linux
Machine Type: i586-mandrake-linux-gnu

Bash Version: 2.05b
Patch Level: 0
Release Status: release


    This is not a bug report. Well, maybe it is, in that it is related
    to a note in the BUGS section of the man page:

       It's too big and too slow.

    Most of the slowness in bash is not intolerable. An exception is
    command substitution, expecially when calling a shell function.

    I can illustrate it with these tests:

f1() {
    echo $(( $1 + 1 ))

f2() {
    F2=$(( $1 + 1 ))

q=`seq 1000`

time for n in $q; do
  num=`expr $n + 1`

time for n in $q; do
  num=`f1 $n`

time for n in $q; do
  f2 $n
time for n in $q; do
  num=$(( n + 1 ))

    The four tests do the same thing with:
      1) variable set with command substitution from external command
      2) variable set with command substitution from function
      3) variable set in the function
      4) in-line code

    Tested with:
       bash 2.05b
       pdksh @(#)PD KSH v5.2.14.2 99/07/13.2
       ksh93 Version M 1993-12-28 m+

$ bash ~/bin/xx.sh
real 0m2.892s   user 0m0.134s   sys 0m2.501s  ## cmd.sub, external command
real 0m2.623s   user 0m0.127s   sys 0m2.290s  ## cmd.sub, function
real 0m0.060s   user 0m0.056s   sys 0m0.002s  ## variable set in function
real 0m0.022s   user 0m0.015s   sys 0m0.006s  ## in-line code

$ pdksh ~/bin/xx.sh
    3.76s real     0.15s user     1.00s system
    2.53s real     0.13s user     0.85s system
    0.03s real     0.03s user     0.00s system
    0.02s real     0.01s user     0.00s system

$ ksh ~/bin/xx.sh    ## ksh93
real   0m2.77s    user   0m0.40s    sys    0m2.24s
real   0m0.09s    user   0m0.08s    sys    0m0.00s
real   0m0.03s    user   0m0.03s    sys    0m0.00s
real   0m0.02s    user   0m0.02s    sys    0m0.00s

    I've run these tests several times; the numbers do not vary much.

    With bash, assigning to a variable using command substitution is
    almost as slow with a function as an external command.

    Is there any hope for an improvement in this area?

        Chris F.A. Johnson                              address@hidden
        address@hidden                http://cfaj.freeshell.org
        address@hidden        http://members.rogers.com/c.f.a.johnson

reply via email to

[Prev in Thread] Current Thread [Next in Thread]