Parallel LINQ in .NET C#: using AsUnordered()
May 9, 2014 Leave a comment
In this post we saw how to keep the order of item processing in a parallel query using the AsOrdered() extension method. We also mentioned that this comes at a slight performance cost.
The effect of using AsOrdered() in combined queries is that the order is restored at every step in the query. The performance cost of restoring the order occurs multiple times:
int[] integerList = new int[100];
for (int i = 0; i < integerList.Length; i++)
{
integerList[i] = i;
}
var result =
integerList.AsParallel().AsOrdered()
.Take(10)
.Select(item => new
{
SourceValue = item,
ResultValue = Math.Pow(item, 2)
});
foreach (var v in result)
{
Console.WriteLine("Source {0}, Result {1}",
v.SourceValue, v.ResultValue);
}
In this example the first 10 items are taken after AsOrdered() is called. AsOrdered() imposes the ordering on the Select subquery as well. Therefore ordering will be performed once again on a subquery that can be performed without ordering. This is not too efficient.
The solution comes with the AsUnordered() extension method:
int[] integerList = new int[100];
for (int i = 0; i < integerList.Length; i++)
{
integerList[i] = i;
}
var result =
integerList.AsParallel().AsOrdered()
.Take(10).AsUnordered()
.Select(item => new
{
SourceValue = item,
ResultValue = Math.Pow(item, 2)
});
foreach (var v in result)
{
Console.WriteLine("Source {0}, Result {1}",
v.SourceValue, v.ResultValue);
}
AsUnordered notifies PLINQ that ordering is not important. The first ten items are taken from the data source after the AsParallel() extension as before. The taken items will then be the basis for the Select subquery which can be performed without ordering.
Conclusion: the AsOrdered and AsUnordered extension methods are applied to all subqueries. In the above example if the Select query is followed by other queries then they will be unordered as well.
View the list of posts on the Task Parallel Library here.