Multiple threads and performance on a single CPU

岁酱吖の 提交于 2020-01-20 02:35:30

问题


Is here any performance benefit to using multiple threads on a computer with a single CPU that does not having hyperthreading?


回答1:


In terms of speed of computation, No. In fact things will slow down due to the overhead of managing the threads.

In terms of responsiveness, yes. You can for example have one thread wait on an IO operation and have another run a GUI at the same time.




回答2:


It depends on your application. If it spends all its time using the CPU, then multithreading will just slow things down - though you may be able to use it to be more responsive to the user and thus give the impression of better performance.

However, if your code is limited by other things, for example using the file system, the network, or any other resource, then multithreading can help, since it allows your application to behave asynchronously. So while one thread is waiting for a file to load from disk, another can be querying a remote webserver and another redrawing the GUI, while another is doing various calculations.

Working with multiple threads can also simplify your business logic, since you don't have to pay so much attention to how various independent tasks need to interleave. If the operating system's scheduling logic is better than yours, then you may indeed see improved performance.




回答3:


You can consider using multithreading on a single CPU

  1. If you use network resources
  2. If you do high-intensive IO operations
  3. If you pull data from a database
  4. If you exploit other stuff with possible delays
  5. If you want to make your app with ultraspeed reaction

When you should not use multithreading on a single CPU

  1. High-intensive operations which do almost 100% CPU usage
  2. If you are not sure how to use threads and synchronization
  3. If your application cannot be divided into several parallel processes



回答4:


Yes, there is a benefit of using multiple threads (or processes) on a single CPU - if one thread is busy waiting for something, others can continue doing useful work.

However this can be offset by the overhead of task switching. You'll have to benchmark and/or profile your application on production grade hardware to find out.




回答5:


Regardless of the number of CPUs available, if you require preemptive multitasking and/or applications with asynchronous components (i.e. pretty much anything that combines a responsive GUI with a non-trivial amount of computation or continuous I/O processing), multithreading performs much better than the alternative, which is to use multiple processes for each application.

This is because threads in the same process can exchange data much more efficiently than can multiple processes, because they share the same memory context.

See this Wikipedia article on computer multitasking for a fairly concise discussion of these issues.




回答6:


Absolutely! If you do any kind of I/O, there is great advantage to having a multithreaded system. While one thread wait for an I/O operation (which are relatively slow), another thread can do useful work.



来源:https://stackoverflow.com/questions/47703/multiple-threads-and-performance-on-a-single-cpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!