Processing parallel data with std.parallelism
Concurrency can be thought of as doing several tasks in the same time frame, hiding the implementation of putting one task on hold while it waits for something and another task is running concurrently. Parallelism, by contrast, is spreading a task out across several processors to be executed simultaneously. Parallelism is ideally suited to numeric computations, where one portion of the result does not immediately depend on another, for example, when multiplying several smaller factors to divide and conquer the multiplication of a large number or performing one calculation on several independent values of an array.
How to do it…
To process parallel data with std.parallelism
, execute the following steps:
Use the command
import std.parallelism;
.Write a normal
foreach
loop that works on an array.Make sure that each loop iteration is independent.
Call
parallel()
on the array you're looping.
Take a look at the following code:
import std.parallelism; import...