全部博文(11)
分类: LINUX
2008-04-13 20:47:48
After reading , I became more interested in the speed of shell operations. In his book, Chris says “Command Substitution Is Slow.” He is correct!
$ f() { echo -n }; time for i in {0..100}; do v=$( f ); done
real 0m4.189s
user 0m0.000s
sys 0m4.188s
$ f() { _F="" }; time for i in {0..100}; do f; v=$_F; done
real 0m0.006s
user 0m0.000s
sys 0m0.000s
I found a few other equivalent operations which can be used to speed up shell scripts to varying degrees (none like the above) depending on the task at hand. As Chris says, “the extra few milliseconds … may not seem significant, but scripts often loop hundred of even thousands of times.”
${#array[@]} is faster than () when expanding an array (#7)
$ a=(); time for i in {0..1000}; do a=(${a[@]} $i);done; echo ${#a[@]}
real 0m3.545s
user 0m3.544s
sys 0m0.000s
1001
$ a=(); time for i in {0..1000}; do a[${#a[@]}]=$i;done; echo ${#a[@]}
real 0m0.043s
user 0m0.040s
sys 0m0.003s
1001
< is faster than cat
$ time for i in {0..10000}; do var=`cat out`;done
real 0m9.328s
user 0m2.892s
sys 0m6.436s
$ time for i in {0..10000}; do var=` real 0m5.930s
user 0m1.412s
sys 0m4.520secho is faster than printf (though not nearly as powerful)
$ time for i in {0..100000}; do printf "\n"; done >/dev/null
real 0m4.446s
user 0m4.076s
sys 0m0.236s
$ time for i in {0..100000}; do echo; done >/dev/null
real 0m3.291s
user 0m3.100s
sys 0m0.184sArithmetic Evaluation is faster than let
$ i=0; time while :; do let "i = i + 1"; [[ $i -gt 100000 ]] && break;donereal 0m8.211s
user 0m7.900s
sys 0m0.304s$ i=0; time while :; do ((i++)); [[ $i -gt 100000 ]] && break;done
real 0m5.287s
user 0m4.980s
sys 0m0.304sUPDATE: This appears to still be true, but by a different margin. See comments.
List expansion is faster than seq and command substitution (though not always available)
$ time for i in $(seq 0 1000000); do :; done
real 0m28.482s
user 0m28.066s
sys 0m0.412s
$ time for i in {0..1000000}; do :; done
real 0m24.563s
user 0m24.402s
sys 0m0.156sUPDATE: On BSD systems the apparent seq equivalent (jot) is faster than list expansion. See comments.
: is faster than true
$ i=0; time while true; do ((i++)); [[ $i -gt 1000000 ]] && break;done
real 0m57.360s
user 0m53.967s
sys 0m3.392s$ i=0; time while :; do ((i++)); [[ $i -gt 1000000 ]] && break;done
real 0m54.138s
user 0m50.571s
sys 0m3.560s