我正在尝试解决优化问题并找到执行以下命令的最有效方法:
whois -> sed -> while (exit while) ->perform action
while循环当前看起来像
while [x eq smth]; do
x=$((x+1))
done
some action
使用if inside(if子句与while相同)可能更有效率。另外,使用bash评估每一步所需时间的最佳情况是什么?
答案 0 :(得分:2)
Bash中迄今为止最大的性能损失和最常见的性能问题是不必要的分叉。
while [[ something ]]
do
var+=$(echo "$expression" | awk '{print $1}')
done
将比
慢几千倍while [[ something ]]
do
var+=${expression%% *}
done
由于前者每次迭代会产生两个叉子,而后者不会导致两个叉子。
导致分叉的事情包括但不限于pipe | lines
,$(command expansion)
,<(process substitution)
,(explicit subshells)
以及使用help
中未列出的任何命令(哪个type somecmd
会识别为“内置”或“#shell”关键字&#39;)。