Hyper-threading versus physical CPU cores
In most cases, hyper-threading is very useful and improves performance, but when you truly maximize CPU usage it is generally better to only use the physical processor count. To demonstrate how this affects the performance, we will run the tests from the previous section again. This time with 1, 2, 4, 8, and 16 processes to demonstrate how it affects the performance. Luckily, the multiprocessing
library has a nice Pool
class to manage the processes for us:
import sys import datetime import multiprocessing def busy_wait(n): while n > 0: n -= 1 if __name__ == '__main__': n = 10000000 start = datetime.datetime.now() if sys.argv[-1].isdigit(): processes = int(sys.argv[-1]) else: print('Please specify the number of processes') print('Example: %s 4' % ' '.join(sys.argv)) sys.exit(1) with multiprocessing.Pool(processes=processes) as pool: ...