宇 雷
宇 雷

Reputation: 67

More threads, slower the speed, even when there are enough CPU cores

I got an application which can start any number of threads, each thread does the same task : going through a vector which contains 5000 messages and then process each one of them.

between threads, there is no resource competion , no race condition at all. and there are 4 cpu Cores on the box where I run my application. no other process is doing any CPU consuming task at the time when I run my application.

however, the result I got is as below.

if there is only one thread running, it took the thread 0.45 seconds to process those 5000 messages.

if there are 4 threads running, it took each thread around o.55 seconds to process those message, more than 20% increase.

if there are more messages to process, say 150,000 messages, then there is no difference between processing time with running 1 thread or 4 threads.

I don't understand what caused the time increasing while there were 4 threads running, there were 4 CPU cores, enough for 4 threads.

and why there was no time increasing while processing time was longer?

I did my test using Linux 2.6.26. the scheduler has been improved since 2.6.18. I did the same test using 2.6.18 too, the result was worse while there were 4 threads running which proves that the scheduler did was improved.

Upvotes: 2

Views: 1300

Answers (1)

mac
mac

Reputation: 5647

Threads bring most benefits in IO-bound processes. However, given the setup you describe a 20% performance decrease is probably a sign that your threads are running into contention on some library/system calls.

Upvotes: 1

Related Questions