2

这是 Bash 变量声明语句:

foo='b'`    `'ar'

效率低于此:

foo='bar'

这种格式化“悬挂”或“对齐”缩进(续行)的方式:

a_long_variable_name='a seemingly infinitely long '`
                    `'string requiring continuatio'`
                    `'n on the next line...'

产生子壳,浪费资源,或以任何方式影响性能,除了惹恼那些不喜欢它的形式(反?)可读性的人?

效率如:计算机效率,而不是“人类键入许多字符并创建基于空间的无偿维护债务”。

能否轻松证明对性能的影响(或缺乏影响)?

4

2 回答 2

5
tester1() {
  for i in {1..1000}; do
    a_long_variable_name='a seemingly infinitely long '`
                        `'string requiring continuation'`
                        `'on the next line...'
    echo $a_long_variable_name > tmp 
  done
}

tester2() {
  for i in {1..1000}; do
    a_long_variable_name="a seemingly infinitely long\
string requiring continuation\
on the next line..."
    echo $a_long_variable_name > tmp 
  done
}

echo tester1
time tester1
echo tester2
time tester2

结果

tester1

real    0m1.878s
user    0m0.209s
sys     0m0.566s

tester2

real    0m0.335s
user    0m0.026s
sys     0m0.078s

这些都具有与案例 2 相似的时间:

read -r -d '' a_long_variable_name <<EOF
    a seemingly infinitely long
    string requiring continuation
    on the next line...
EOF

a_long_variable_name="a seemingly infinitely long\
                      string requiring continuation\
                      on the next line..."
于 2013-03-21T01:24:49.790 回答
0

如果我需要它,我可能会这样构建字符串:

x='a seemingly infinitely long '
x="$x"'string requiring continuatio'
x="$x"'n on the next line...'

a_long_variable_name="$x"

或该主题的细微变化。或者我会在每一行重复长变量名。这是我使用的真实脚本——它为我列出了相关的环境变量:

informix1="DB[^=]|DELIMIDENT=|SQL|ONCONFIG|TBCONFIG|INFOR"
informix2="CLIENT_LOCALE=|GL_|GLS8BITSYS|CC8BITLEVEL|ESQL|FET_BUF_SIZE="
informix3="INF_ROLE_SEP=|NODEFDAC=|ONCONFIG|OPTCOMPIND|PDQ|PSORT"
informix4="PLCONFIG|SERVER_LOCALE|FGL|C4GL|NE_"
informix5="TCL_LIBRARY|TK_LIBRARY|TERM=|TERMCAP=|TERMINFO="
informix="$informix1|$informix2|$informix3|$informix4|$informix5"
system1="COLLCHAR=|LANG=|LC_"
system2="(DY)?LD_LIBRARY_PATH(_[63][42])?=|PATH=|SHLIB_PATH=|LIBPATH="
system="$system1|$system2"
jlss="IX([A-Z]|D(32|64)?)="

env |
${EGREP:-egrep} "^($informix|$system|$jlss)" |
sort

(当 Mac OS X 10.7.x因为它“太大”而停止处理表达式时,切换使用哪个egrep程序的能力变得必要。)egrep

于 2013-03-21T14:09:39.240 回答