Home > OS >  Golang - Concurrency vs Parallelism vs Sequential
Golang - Concurrency vs Parallelism vs Sequential

Time:11-24

I am learning Go at the moment and got quite frustrated understanding the different between Concurrency vs Parallelism vs Sequential.

Let's say we have a process that scraps a slice of 5 URLs and paste the content in a text file. The process takes 2 seconds per URL.

  • Sequentially -> it takes 10 seconds since it does one after the other
  • Parallel -> takes less than 10 seconds since it does them simultaneously but using multiple threads or processors.
  • Concurrently -> takes less than 10 seconds but it does not require multiple threads or processors.

Not sure if I am right until here. My questions are:

I read that parallelism does things simultaneously (running and listening to music for example) and concurrency handles things at the same time (getting breakfast done while ironing shirts for example).

But if that's the case, why is concurrency not taking 10 seconds to complete since at the end of the day you are not doing everything at the same time but just doing bits of everything until you complete it all?

CodePudding user response:

Here's an analogy to explain.

You need to fry 5 eggs, sunny side up. To cook an egg you crack it onto the griddle, wait for a few minutes, then take it off.

  • The sequential approach is to fry the first egg to completion, then fry the second egg to completion, and so on, until you have 5 fried eggs.

  • The parallel approach is to hire 5 cooks, tell each of them to fry an egg, and wait until they are all finished.

  • The concurrent approach is that you cook all 5 eggs yourself the way you would actually do it. That is, you quickly crack each egg onto the pan, then take each one off when it's ready.

The reason you're able to save time without having to hire 5 cooks is because the number of cooks wasn't what was limiting you from going faster. It takes a couple minutes to cook an egg, but it only occupies your attention and your hands for a few seconds at the beginning and end.

The Go runtime and modern OS runtimes are similarly smart. They know that while your thread is waiting to receive a network response, the processor can look for other things to occupy it's attention.

The larger picture of concurrency is concerned not primarily with the number of processors, but with resource contention in general. The execution of tasks demands resources, and we cannot use more resources than are available. Processors are one resource, but there is also memory storage, memory bandwidth, network bandwidth, file handles, and the list goes on.

  • Related