Pradeep Sanjeewa
Pradeep Sanjeewa

Reputation: 2222

Why performance is improved when thread count is increased than the hardware-possible thread count?

In my PC, I have 16 logical cores(multiprocessing.cpu_count()=16). I have an application which has around 2000 independent processes. I used multi-threading with different thread counts and the results are as follows.


Threads | Total Time(s)

8 | 4225 (~ 70 min)

16 | 2733 (~ 46 min)

32 | 2156 (~ 35 min)

64 | 2123 (~ 35 min)

Theoretically, I should get the best results when thread count is 16. And I should get the same results even if the thread count is increased above 16. But I'm getting the best results when the thread count is increased to 32. And the results looks to be unchanged above that count. What is the reason for that?

Upvotes: 1

Views: 658

Answers (1)

Amadan
Amadan

Reputation: 198314

Strictly speaking, Python cannot run more than one thread concurrently (because of GIL), so any threads that run Python code at 100% CPU usage (e.g. pure calculation) will always degrade in performance with more threads.

Getting any performance increases from threads means you have some I/O in your code that is being waited on, which makes your thread's CPU utilisation fall under 100%. Increasing the number of threads allows the CPU to fill up those gaps.

Upvotes: 3

Related Questions