Motivation
No matter what programming language you’re using, sometimes you get itch to map a whole collection to another. Usually, you do things like:
or in C#:
or like in Python:
The main problem with above codes is that when the size of the collection grows, or the mapping itself becomes more complex, this operation will be very time-take. And that is because those codes are running in one thread.
Since all the items in the collection are mapped in the same way, why don’t we just put them onto threads and let them to be done seperatly?
Go Parallel
Parallel operations on collections is very easy in popular languages. The operation like this in most language will allow system to distribute thread resource according to the current status of CPU, and how much the task is, which could be extremely overwhelming if we write it our own.
usecase: To make it more practical, let’s say we want to download all the images based on a list, which contains 10000 urls of those images.
C#
To download these images, it could be a little complex in C#. C# has a foreach
key word used to make list traversal like this:
to do that in parallel way, we use the parallel version of foreach
, like this:
One thing you should be careful about is that in C# you can not map collections directly. You will have to put all the items in a parallel version of list, which is
ConcurrentBag
here, and it is thread safe. You can refer to another way by using ParallelLoopResult.
Python
Things could be even more easier in Python:
Please make sure you have imported the right library of pool.
scala
In scala, it is super easy to do the same thing, and you literally only one line to do that:
And done!
Of course you can define your own function to modify the string, but be sure this function is thread safe.