How many CPU cores can you actually use in parallel?
When you’re running a CPU-intensive parallel program, you often want to have a thread or process pool sized by the number of CPU cores on your machine.
Fewer threads and you’re not taking advantage of all the cores, more than that and your program will start running slower as multiple threads compete for the same core.
Or that’s the theory, anyway.
So how do you check how many cores your computer has?
And is this actually good advice?
It turns out to be surprisingly tricky to nail down how many threads to run:
- The Python standard library provides multiple APIs to get this info, but none are sufficient.
- Even worse, because of CPU features like instruction-level parallelism and simultaneous threading (aka Hyper-threading on Intel CPUs), the number of