0

我正在尝试解决优化问题并找到执行以下命令的最有效方法:

whois -> sed -> while (exit while) ->perform action

while 循环目前看起来像

while [x eq smth]; do
x=$((x+1))
done

some action

在 if 内部(if 子句与 while 相同)中使用 while true 可能更有效。此外,使用 bash 评估每一步所需时间的最佳情况是什么?

4

2 回答 2

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), 以及使用未列出的任何命令helptype somecmd将标识为“内置”或“shell 关键字”)。

于 2013-02-11T17:36:01.683 回答
0

对于初学者来说,您可以删除$(,这会创建一个子shell,并且肯定会稍微减慢任务速度

while [ x -eq smth ]
do
  (( x++ ))
done
于 2013-02-11T17:30:13.197 回答