PLINQ

PLINQ (Parallel LINQ, pronounced “plink”) allows a program to execute LINQ queries across multiple processors or cores in a multi-core system. If you have a multi-core CPU and a nicely parallelizable query, PLINQ may improve your performance considerably.

So what kinds of queries are “nicely parallelizable”? The short, glib answer is, it doesn’t really matter. Microsoft has gone to great lengths to minimize the overhead of PLINQ, so using PLINQ may help for some queries and shouldn’t hurt you too much for queries that don’t parallelize nicely.

Simple queries that select items from a data source often work well. If the items in the source can be examined, selected, and otherwise processed independently, then the query is parallelizable.

Queries that must use multiple items at the same time do not parallelize nicely. For example, adding an OrderBy function to the query forces the program to gather all of the results before sorting them so that part of the query at least will not benefit from PLINQ.


THE NEED FOR SPEED
Some feel that adding parallelism to LINQ is kind of like giving caffeine to a snail. A snail is slow. Giving it caffeine might speed it up a bit, but you’d get a much bigger performance gain if you got rid of the snail and got a cheetah instead.
Similarly, LINQ isn’t all that fast. Adding parallelism will speed it up but you will probably get a larger speed improvement by moving the data into a database or using special-purpose algorithms designed to manage your particular data.
This argument is true, but you don’t use LINQ because it’s fast; you use it because it’s convenient, easy to use, and flexible. Adding parallelism makes it a bit faster and, as you’ll see shortly, is so easy that it doesn’t cost you much effort.
If you really need significant performance improvements, you should consider moving the data into a database or more sophisticated data structure, but if you’re using LINQ anyway, you may as well take advantage of PLINQ when you can.

Adding parallelism to LINQ is remarkably simple. First, add a reference to the System.Threading library to your program. Then add a call to AsParallel to the enumerable object that you’re searching. For example, the following code uses AsParallel to select the even numbers from the array numbers:

Dim evens =
    From num In numbers.AsParallel()
    Where num Mod 2 = 0

PUZZLING PARALLELISM
Note that for small enumerable objects (lists containing only a few items) and on computers that have only a single CPU, the overhead of using AsParallel may actually slow down execution slightly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.91.2