15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Parallel linQ ❘ 289<br />

Console.WriteLine();<br />

Of course the result now looks like this:<br />

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20<br />

The Range() method does not return a collection fi lled with the values as defi ned.<br />

This method does a deferred query execution similar to the other methods. The<br />

method returns a RangeEnumerator that just does a yield return with the values<br />

incremented.<br />

You can combine the result with other extension methods to get a different result — for example, using the<br />

Select() extension method:<br />

var values = Enumerable.Range(1, 20).Select(n = > n * 3);<br />

The Empty() method returns an iterator that does not return values. This can be used for parameters that<br />

require a collection where you can pass an empty collection.<br />

The Repeat() method returns an iterator that returns the same value a specifi c number of times.<br />

Parallel linq<br />

.<strong>NET</strong> 4 contains a new class ParallelEnumerable in the System.Linq namespace to split the work<br />

of queries across multiple threads. Although the Enumerable class defi nes extension methods to the<br />

IEnumerable < T > interface, most extension methods of the ParallelEnumerable class are extensions for<br />

the class ParallelQuery < TSource > . One important exception is the AsParallel() method that extends<br />

IEnumerable < TSource > <strong>and</strong> returns ParallelQuery < TSource > , so a normal collection class can be<br />

queried in a parallel manner.<br />

Parallel queries<br />

To dmonstrate Parallel LINQ, a large collection is needed. With small collections you will not see any<br />

effect when the collection fi ts inside the CPU ’ s cache. In the following code, a large int array is fi lled with<br />

r<strong>and</strong>om values:<br />

const int arraySize = 100000000;<br />

var data = new int[arraySize];<br />

var r = new R<strong>and</strong>om();<br />

for (int i = 0; i < arraySize; i++)<br />

{<br />

data[i] = r.Next(40);<br />

}<br />

code snippet ParallelLinqSample/Program.cs<br />

Now you can use a LINQ query to fi lter the data <strong>and</strong> get a sum of the fi ltered data. The query defi nes a fi lter<br />

with the where clause to summarize only the items with values 20 < , <strong>and</strong> then the aggregation function<br />

sum is invoked. The only difference to the LINQ queries you ’ ve seen so far is the call to the AsParallel()<br />

method.<br />

var sum = (from x in data.AsParallel()<br />

where x < 20<br />

select x).Sum();<br />

As with the LINQ queries you ’ ve seen so far, the compiler changes the syntax to invoke the methods<br />

AsParallel() , Where() , Select() , <strong>and</strong> Sum() . AsParallel() is defi ned with the ParallelEnumerable<br />

class to extend the IEnumerable < T > interface, so it can be called with a simple array. AsParallel()<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!