Does multi-threading improve performance? How?

后端 未结 2 1056
萌比男神i
萌比男神i 2020-12-03 08:54

I hear everyone talking about how multi-threading can improve performance. I don\'t believe this, unless there is something I\'m missing. If I have an array of 100 elements

相关标签:
2条回答
  • 2020-12-03 09:16

    For CPU bound tasks where you have more than one core in your processor you can divide your work on each of your processor core. If you have two cores, split the work on two threads. This way you have to threads working at full speed. However threads are really expensive to create, so you need a pretty big workload to overcome the initial cost of creating the threads.

    You can also use threads to improve appeared performance (or responsiveness) in an interactive application. You run heavy computations on a background thread to avoid blocking UI interactions. Your computations does not complete faster, but your application does not have those "hangs" that make it appear slow and unresponsive.

    0 讨论(0)
  • 2020-12-03 09:21

    For a simple task of iterating 100 elements multi-threading the task will not provide a performance benefit.

    Iterating over 100 billion elements and do processing on each element, then the use of additional CPU's may well help reduce processing time. And more complicated tasks will likely incur interrupts due to I/O for example. When one thread is sleeping waiting for a peripheral to complete I/O (e.g. a disk write, or a key press from the keyboard), other threads can continue their work.

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