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 ...

Get Mastering Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.