Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Multi threading increase the speed of the calculation on Single Processor

Tags:

On a single processor, Will multi-threading increse the speed of the calculation. As we all know that, multi-threading is used for Increasing the User responsiveness and achieved by sepating UI thread and calculation thread. But lets talk about only console application. Will multi-threading increases the speed of the calculation. Do we get culculation result faster when we calculate through multi-threading.

what about on multi cores, will multi threading increse the speed or not.

Please help me. If you have any material to learn more about threading. please post.

Edit: I have been asked a question, At any given time, only one thread is allowed to run on a single core. If so, why people use multithreading in a console application.

Thanks in advance, Harsha

like image 886
Harsha Avatar asked May 18 '10 10:05

Harsha


People also ask

Does multithreading benefit a computer with only one processor?

Yes you can do multithreading on a single processor system. In multi-processor system , multiple threads execute , simultaneously on different cores. Eg- If there are two threads and two cores , then each thread would run on individual core.

Does multithreading improve performance on a single core?

Even on a single-core platform, multithreading can boost the performance of such applications because individual threads are able to perform IO (causing them to block), while others within the same process continue to run.

How multithreading improves performance over a single threaded solution?

In a multiprocessor architecture, each thread can run on a different processor in parallel using multithreading. This increases concurrency of the system. This is in direct contrast to a single processor system, where only one process or thread can run on a processor at a time.

Can multithreading be done on a single processor?

Concurrency and ParallelismIn a multithreaded process on a single processor, the processor can switch execution resources between threads, resulting in concurrent execution. Concurrency indicates that more than one thread is making progress, but the threads are not actually running simultaneously.


2 Answers

In general terms, no it won't speed up anything.

Presumably the same work overall is being done, but now there is the overhead of additional threads and context switches.

On a single processor with HyperThreading (two virtual processors) then the answer becomes "maybe".

Finally, even though there is only one CPU perhaps some of the threads can be pushed to the GPU or other hardware? This is kinda getting away from the "single processor" scenario but could technically be way of achieving a speed increase from multithreading on a single core PC.

Edit: your question now mentions multithreaded apps on a multicore machine. Again, in very general terms, this will provide an overall speed increase to your calculation. However, the increase (or lack thereof) will depend on how parallelizable the algorithm is, the contention for memory and cache, and the skill of the programmer when it comes to writing parallel code without locking or starvation issues.

like image 101
Coxy Avatar answered Oct 26 '22 04:10

Coxy


Few threads on 1 CPU:

  • may increase performance in case you continue with another thread instead of waiting for I/O bound operation
  • may decrease performance if let say there are too many threads and work is wasted on context switching

Few threads on N CPUs:

  • may increase performance if you are able to cut job in independent chunks and process them in independent manner
  • may decrease performance if you rely heavily on communication between threads and bus becomes a bottleneck.

So actually it's very task specific - you can parallel one things very easy while it's almost impossible for others. Perhaps it's a bit advanced reading for new person but there are 2 great resources on this topic in C# world:

  • Joe Duffy's web log
  • PFX team blog - they have a very good set of articles for parallel programming in .NET world including patterns and practices.
like image 24
Andrey Taptunov Avatar answered Oct 26 '22 02:10

Andrey Taptunov