Does multi-threading improve performance? How?
Asked Answered
S

2

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?

Staten answered 8/3, 2013 at 11:11 Comment(3)
Look at this one : #2856739Isbell
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.Basham
So it's not possible for a one single-threaded program to use all cores?Staten
T
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.

Thereabouts answered 8/3, 2013 at 11:18 Comment(2)
+1 for mentioning I/O performance - the main reason why preemptive mutitaskers were built in the first place.Disagree
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.Lindquist
E
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.

Evered answered 8/3, 2013 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.