pythonmultithreadingcpu

CPU cores, threads, and optimal number of workers in Python


I am beginning to appreciate the usefulness of the threading library on python and I was wondering what was the optimal number of threads to keep open in order to maximise the efficiency of the script I am running.

Please do keep in mind that my one and only priority is speed, I don't need to multitask /do other stuff (think a computing dedicated server)

To be specific, if I run on a 4 cores / 4 threads CPU, will the optimal number of threads be 4? 8? 16? Then again, if I had more than a thread per core (4 core 8 t), would the answer change? Also, does cpu clock influence any of this?

I understand there are a variety of implications on the matter but, as much as my research on the subject went, I still feel to be very much in the dark. (I gathered it's not so simple as n threads = n processes)


Solution

  • The question is very complex, because on the same CPU there can be an arbitrary number of others threads running, from processes that are not under your control.

    Instead, you can estimate when a certain piece of code worth to be executed by a separate thread, based on the time needed to create a new thread.