views:

607

answers:

9

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

+12  A: 

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.

Coxy
Note that this holds for CPU intensive tasks, for tasks doing blocking operations(IO bound/talks to a database/etc.) multithreading can speed things up, though doing async or non-blocking operations can speed things further up, but not all apis have non-blocking variants, and those who do can be harder to program.
nos
This isn't completely true, after all doing parallel compiles is almost always faster no matter what the hardware is
Earlz
+1  A: 

No, no and no.

Unless you write parallelizing code to take advantage of multicores, it will always be slower if you have no other blocking functions.

leppie
As @coxymla points out there might be a CPU with HyperThreading. This might in fact increase the speed of execution, even if the CPU has only a single core.
0xA3
Actually, you're not right here =/
Artiom Chilaru
Thanks for the reply, Lets forget Multicores for time being. I have been asked questions like, If calculation didn't speed, whey we have multithreading on single core. I have explained them that, multithreading is for responsiveness, communication related, DB related work. But many people believe that multi-threading increses the speed of calculation. like to know how. Thanks for your time.
Harsha
@0xA3: Hyperthreading implies multicores...
leppie
@Harsha, @leppie: HyperThreading works for single cores. It uses CPU "idle" times for pseudo-parallel execution. From http://en.wikipedia.org/wiki/Hyperthreading: When execution resources would not be used by the current task in a processor without hyper-threading, and especially when the processor is stalled, a hyper-threading equipped processor can use those execution resources to execute another scheduled task. (The processor may stall due to a cache miss, branch misprediction, or data dependency.)
0xA3
@Artiom Chilaru: Please elaborate, thanks.
leppie
@leppie, it really depends on the kind of algorithm they are going to use there. Some algorithms can be split into multiple independent calculation "branches" in which case the whole process can be greatly improved in a multi-core scenario. I wouldn't just cut it down to a flat "no", unless we know the specifics.
Artiom Chilaru
@Artiom Chilaru: I do understand what you are saying, but I thought I covered those aspects in my answer.
leppie
Well, I do believe that when he states "Do we get culculation result faster when we calculate through multi-threading.", it implies that the calculation algorithm will be written to take advantage of multi-threading. Which is why your direct "no" threw me off like this :)
Artiom Chilaru
+2  A: 

If the task is compute bound, threading will not make it faster unless the calculation can be split in multiple independent parts. Even so you will only be able to achieve any performance gains if you have multiple cores available. From the background in your question it will just add overhead.

However, you may still want to run any complex and long running calculations on a separate thread in order to keep the application responsive.

Brian Rasmussen
+1 For the simple sane answer :)
Rusty
+6  A: 

What is your calculation doing? You won't be able to speed it up by using multithreading if it a processor bound, but if for some reason your calculation writes to disk or waits for some other sort of IO you may be able to improve performance using threading. However, when you say "calculation" I assume you mean some sort of processor intensive algorithm, so adding threads is unlikely to help, and could even slow you down as the context switch between threads adds extra work.

Steve Haigh
+1  A: 

Exactly like the user input example, one thread might be waiting for a disk operation to complete, and other threads can take that CPU time.

deltreme
+4  A: 

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 indepent chunks and process them in independent manner
  • may decrease pefromance if you rely heavily on communication between threads and bus becomes a botleneck.

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:

Andrei Taptunov
+1  A: 

As described in the other answers, multi-threading on a single core won't give you any extra performance (hyperthreading notwithstanding). However, if your machine sports an Nvidia GPU you should be able to use the CUDA to push calculations to the GPU. See http://www.hoopoe-cloud.com/Solutions/CUDA.NET/Default.aspx and http://stackoverflow.com/questions/1249892/c-perform-operations-on-gpu-not-cpu-calculate-pi.

ebpower
A: 

Above mention most.

Running multiple threads on one processor can increase performance, if you can manage to get more work done at the same time, instead of let the processor wait between different operations. However, it could also be a severe loss of performance due to for example synchronization or that the processor is overloaded and cant step up to the requirements.

As for multiple cores, threading can improve the performance significantly. However, much depends on finding the hotspots and not overdo it. Using threads everywhere and the need of synchronization can even lower the performance. Optimizing using threads with multiple cores takes a lot of pre-studies and planning to get a good result. You need for example to think about how many threads to be use in different situations. You do not want the threads to sit and wait for information used by another thread.

http://www.intel.com/intelpress/samples/mcp_samplech01.pdf
https://computing.llnl.gov/tutorials/parallel_comp/
https://computing.llnl.gov/tutorials/pthreads/
http://en.wikipedia.org/wiki/Superscalar
http://en.wikipedia.org/wiki/Simultaneous_multithreading

Fredrik_jakob
Hello Jakob, Thanks for the posting the answer and PDF File. I have couple of requests/questions:1. Do you any project files or source code, through which one can learn multi threading on single processor for increasing the performance. If you have any material or link for this question. 2.Do you have any Threading Guidelines, any link please send me. 3. Do you have any Threading debug tools. Thanks for the your time and posting answer.
Harsha
Hello!I'm sorry but I do not have any source code. Much of what decides how your program work depends on how the architecture of your CPU is. I don't have any source code or debug tool, sorry for that, but I've edited my post with some more links!
Fredrik_jakob
A: 

Even a CPU bound task might run faster multi-threaded if properly designed to take advantage of cache memory and pipelineing done by the processor. Modern processors spend a lot of time twiddling their thumbs, even when nominally fully "busy".

Imagine a process that used a small chunk of memory very intensively. Processing the same chunk of memory 1000 times would be much faster than processing 1000 chunks of similar memory.

You could certainly design a multi threaded program that would be faster than a single thread.

ddyer