13

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 and traversing it takes 6 seconds. When I divide the work between two threads, the processor would have to go through the same amount of work and therefore time, except that they are working simultaneously but at half the speed. Shouldn't multi threading make it even slower? Since you need additional instructions for dividing the work?

Valentin
  • 654
  • 2
  • 7
  • 15
  • 3
    Look at this one : http://stackoverflow.com/questions/2856239/will-multi-threading-increase-the-speed-of-the-calculation-on-single-processor – Chris Mar 08 '13 at 11:20
  • If you have 4 processors for example (which is quite common nowadays, even in smart phones), and your program only has one thread, it will only have access to 25% of the available CPU resources. If you only have one processor, it will not make a difference. – assylias Mar 08 '13 at 13:41
  • So it's not possible for a one single-threaded program to use all cores? – Valentin Mar 08 '13 at 15:47

2 Answers2

20

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.

suspectus
  • 16,548
  • 8
  • 49
  • 57
  • 1
    +1 for mentioning I/O performance - the main reason why preemptive mutitaskers were built in the first place. – Martin James Mar 08 '13 at 14:13
  • Many desktop devices doesn't support multithreaded use. On example better disks have two non blocking channels: for read and for write. It means that only one thread can write at once effectively. Server hardware can have multiple channels: single disk can have few SATA connections or can be used in RAID. In this case, multiple threads is more effective. – Pingwin Tux Oct 11 '14 at 07:33
7

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.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108