我有 4 個命令,它們在嵌套的 for 回圈體中都相互依賴(因此它們應該按順序執行)。我想加快執行時間,所以我想將這 4 個命令與接下來的 4 個命令并行運行,依此類推。我已經讀過&和wait應該在這種情況下使用。但是,我不確定將它們放在哪里,因為就我所知,將它們放在 4 個命令中的每一個之后都不會加快任何速度。
我有大約 10 個內核可以使用。
for p in {50,60,70,80,90,100,200,300}
do
for min_p_len in {1,2,3,4,5}
do
for min_s_rem in {1,2,3}
do
for length_threshold in {2,3,4}
do
command 1
command 2
command 3
command 4
done
done
done
done
uj5u.com熱心網友回復:
將您的內回圈替換為:
for length_threshold in {2,3,4}
do
{
command 1
command 2
command 3
command 4
} &
done
這可能會將您的系統推向其性能極限。
uj5u.com熱心網友回復:
為了更好地管理后臺行程,請使用 xargs。使用 stdin 將狀態傳遞給 xargs。
for p in {50,60,70,80,90,100,200,300}
do
for min_p_len in {1,2,3,4,5}
do
for min_s_rem in {1,2,3}
do
for length_threshold in {2,3,4}
do
# output variables on one line properly qouted
declare -p p min_p_len min_s_rem length_threshold | base64 -w0
done
done
done
done |
{
work() {
eval "$(base64 -d <<<"$1")" # read the variables from arguments
command 1
command 2
command 3
command 4
}
export -f work
xargs -d '\n' -P $(nproc) -n1 bash -c '"$@"' -- work
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/393519.html
上一篇:成對比較檔案夾的所有檔案
