The Difference Of Concurrency And Parallelism
Having multiple processor is really usefull these days. It can be used to process Most desktop or laptop usually bring 2 or more cores with them. But most (beginner) programmer rarely take advantage from it
Concurrency : performing 2 or more task at the same time frame
Lets start with concurrency first, just like what the heading says, Its about performing 2 or more task at the same time. Here is some definisition from wikipedia
Concurrency refers to the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or in partial order, without affecting the final outcome. - Wikipedia
There is a key point here "out-of-order" thats mean it doesn't need to wait until the last process or program to end. Its really usefull when you have many task to compute at the same time. Another analogy is when you have to make a web app that has to be able to handle million request per-second.
Lets say you just write a web app that get 100 request at the same time and your web app takes 1 second to process a request (Its so slow i know). If you don't take advantage of concurrent programming then you pretty much screwd up. Your 100th visitor has to wait 100 second before it started getting respond from your web app, which probably they already leave your site anyway. But if you implement concurrent programming in your web app, your customer might be tricked into thinking that your website is actually fast that it started responding to their request immediately eventhough the throughput is still 1 request per-second.
The main aim of concurrency is the ability to interruptability so processor can interleave between 2 or more task at the same time, resulting in more responsive program.
Parallelism : performing 2 or more task at one time
Now lets talk about parallelism. Lets begin the definition of parallelism shall we ?
The term Parallelism refers to techniques to make programs faster by performing several computations at the same time.- Haskell
Notice the key points here "make program faster" and "several computation at the same time". Parallelism is all about how to split your program into some independent sub program that can run together with your main program (Not to be confused with forking). You can achieve this by performing multi threading. When you create a program, there will be a main thread that running in your program. When you perform compute intensive operation in a function your main thread will not respond to your input until its done. This is ofcourse something that you don't want if your app should be doing lots of interaction with the user.
Lets say you just write a text editor app. If you just write it procedurally without fancy technique it probably consiste of main thread looping continiously waiting for user input. When there is large input, your program will likely be freeze for some time until its done. However if you split it into 2 thread, one for processing in general and one for processing graphics. Your program can busily process the input while the render thread (the one processing graphic) will keep updating it state every time to give responsive feeling to the user. However it doesn't really mean that your program is working concurrently but it gives you some benefit from concurrency.
Now lets say you have large amount of data to be sorted, and now you are using merge sort to sort the data. If your data is only small then a single thread will do, but if there is so many data to be sorted like 264 data then using multi thread approach might increase your sorting speed, here is some table from a paper a found about multi threaded sorting.
As you can see here that it become faster to sort with multiple thread.