Threading vs single thread

前端 未结 8 952
清歌不尽
清歌不尽 2020-12-04 13:32

Is it always guaranteed that a multi-threaded application would run faster than a single threaded application?

I have two threads that populates data from a data so

相关标签:
8条回答
  • 2020-12-04 14:20

    I've seen real-world examples where code has performed so badly with more processors added (horrible lock contention amongst threads) that the system needed to have processors removed to restore performance; so yes, it is possible to make code work worse by adding more threads of execution.

    IO constrained apps are another good example, mentioned above.

    0 讨论(0)
  • 2020-12-04 14:23

    No, it is not. Because when you do multi-threading, your CPU has to switch between thread, memory, register, and that costs. There are some tasks which are divisible like merge sort, but there are some tasks may not be divisible to sub tasks like checking if a number is a prime or not (it is just my sudden example), and then if you try to separate it out, it just runs like a single thread problem.

    0 讨论(0)
提交回复
热议问题