在多CPU上編譯Linux內核時可以用 make -jn 多個任務並行編譯加快速度。印象中在某個文檔看到過 n 選擇為 ncpu + 1,但不清楚理論依據何在。查了一下也沒看到這個說法的原始來源,因此在一個四核的CPU上做了一下簡單的測試,結論是在 n 等於 CPU 個數時的速度最快。下面是結果數據:
n system user real 1 52.477 662.534 693.391 2 48.227 606.096 318.699 3 40.066 500.220 184.886 4 38.619 474.832 140.242 5 38.360 479.650 141.821 6 37.721 480.068 141.241 7 38.188 481.979 141.872 8 38.685 483.338 142.171
用到的腳本如下(為降低其他因素的干擾,所有的文件及編譯過程都放在內存之中進行):
#!/bin/sh tar -C /dev/shm -xjf linux-2.6.34.tar.bz2 mount -o remount,exec /dev/shm pushd /dev/shm/linux-2.6.34 for i in `seq 1 8`; do [ -d ../${i} ] && rm -fr ../${i}/* || mkdir ../${i} cp /boot/config ../${i}/.config time make O=../${i} -j ${i} oldconfig > /dev/null time make O=../${i} -j ${i} > /dev/null rm -fr ../${i} done popd mount -o remount,noexec /dev/shm