Erm...
case "$1" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
scriptname=$1
shift
. $scriptname
)
;;
*)
"$@"
;;
esac
OPTIMISATION FAIL.
NP: Roseland NYC Live, Portishead
Posted by joey hess at Mon Jun 23 18:38:15 2008:
Posting FAIL to blogs w/o analysis is always asking for trouble or a waste of time :-)
In this case, while a subshell is still forked, the shell does avoid the overhead of restarting the ssh interpreter. Not likely to save a vast amount of time for a few dozen init scripts, but not a total FAIL AFAICS.
Posted by Anonymous at Mon Jun 23 19:38:05 2008:
In this case, while a subshell is still forked, the shell does avoid the overhead of restarting the ssh interpreter. Not likely to save a vast amount of time for a few dozen init scripts, but not a total FAIL AFAICS.
I've benchmarked this, actually. Sourcing rather than exec'ing saves significant amounts of time.
$ cat target.sh
#!/bin/bash
$ cat run.sh
#!/bin/bash
./target.sh
$ cat exec.sh
#!/bin/bash
exec ./target.sh
$ cat source.sh
#!/bin/bash
. target.sh
$ time for i in $(seq 1 10000) ; do ./run.sh ; done
real 0m36.046s
user 0m17.517s
sys 0m17.833s
$ time for i in $(seq 1 10000) ; do ./exec.sh ; done
real 0m32.856s
user 0m21.301s
sys 0m9.937s
$ time for i in $(seq 1 10000) ; do ./source.sh ; done
real 0m19.116s
user 0m6.332s
sys 0m7.924s
Posted by daniels at Mon Jun 23 19:52:39 2008:
Posted by Anonymous at Mon Jun 23 19:57:25 2008:
$ cat target.sh
#!/bin/bash
$ cat run.sh
#!/bin/bash
./target.sh
$ cat exec.sh
#!/bin/bash
exec ./target.sh
$ cat source.sh
#!/bin/bash
. target.sh
$ time for i in $(seq 1 10000) ; do ./run.sh ; done
real 0m36.046s
user 0m17.517s
sys 0m17.833s
$ time for i in $(seq 1 10000) ; do ./exec.sh ; done
real 0m32.856s
user 0m21.301s
sys 0m9.937s
$ time for i in $(seq 1 10000) ; do ./source.sh ; done
real 0m19.116s
user 0m6.332s
sys 0m7.924s
No, just PASTE FAIL and lack of preview. :)
$ cat subshell.sh
\#!/bin/bash
( . ./target.sh )
$ time for i in $(seq 1 10000) ; do ./subshell.sh ; done
real 0m23.406s
user 0m8.237s
sys 0m13.869s
Still a big win compared to exec.
Posted by Ross at Mon Jun 23 20:19:36 2008:
$ cat subshell.sh
\#!/bin/bash
( . ./target.sh )
$ time for i in $(seq 1 10000) ; do ./subshell.sh ; done
real 0m23.406s
user 0m8.237s
sys 0m13.869s
Still a big win compared to exec.