# Detect how many CPU cores are available
cores=$( (nproc --all || sysctl -n hw.ncpu) 2>/dev/null || echo 1 )
This command will detect the number of CPU cores available on either Linux (nproc) or BSD-like systems (sysctl), including FreeBSD and macOS, and just fall back to 1 on all other systems, which is the same behavior the script currently has, where only one core is used.
And then replace every make with make -j "$cores" and the entire script runs almost 4 times faster on my system.
Just add this at the very top of your script:
This command will detect the number of CPU cores available on either Linux (nproc) or BSD-like systems (sysctl), including FreeBSD and macOS, and just fall back to
1
on all other systems, which is the same behavior the script currently has, where only one core is used.And then replace every
make
withmake -j "$cores"
and the entire script runs almost 4 times faster on my system.