Multiple threads and performance on a single CPU

后端 未结 6 2013
清酒与你
清酒与你 2020-12-15 07:17

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

6条回答
  •  被撕碎了的回忆
    2020-12-15 07:35

    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.

提交回复
热议问题