Bash script to find best number of threads for make's -j option

Alec Jacobson

November 16, 2012

weblog/

The make command lets you specify a number of processes to run commands on using the -j option. I've read of a heuristic to use 1.5 times the number of cores on your machine. To find out if this is true I wrote a short script:
#!/bin/bash

for i in {1..20}
do
  make clean &>/dev/null
  printf "$i  "
  t=`(time make -j$i &>/dev/null) 2>&1 | grep real | sed -e "s/real[^0-9]*//g"`
  echo "$t"
done
For my project on my iMac with an intel i7 this prints:
1  1m1.235s
2  0m32.128s
3  0m23.353s
4  0m19.989s
5  0m18.007s
6  0m16.613s
7  0m15.490s
8  0m15.644s
9  0m16.307s
10  0m16.415s
11  0m16.861s
12  0m17.535s
13  0m17.053s
14  0m17.112s
15  0m17.550s
16  0m17.267s
17  0m17.274s
18  0m17.618s
19  0m17.540s
20  0m17.653s
Obviously you could improve the accuracy of these timing by running multiple times and averaging.