Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Use the command import std.parallelism;.

  2. Write a normal foreach loop that works on an array.

  3. Make sure that each loop iteration is independent.

  4. Call parallel() on the array you're looping.

Take a look at the following code:

import std.parallelism;
import...