C# AsParallel Example

Threads illustration

Time-consuming query expressions can be run in parallel. With the AsParallel extension method, we enable parallel threads to improve performance. We demonstrate a method that becomes twice as fast on a dual-core machine when AsParallel is used.

This C# tutorial shows how to use the AsParallel extension method from System.Linq.

Key point: AsParallel can make certain queries much faster, and other queries much slower. It depends on the target machine and the characteristics of the query and your data source.

Example

We introduce two methods: SumDefault, which sums all the elements in the array with the Sum method alone; and SumAsParallel, which first calls AsParallel on the array and then calls Sum on the result of AsParallel. In Main, we show that the two methods have the exact same result.

Program that uses AsParallel [C#]

using System;
using System.Diagnostics;
using System.Linq;

class Program
{
    static int SumDefault(int[] array)
    {
	/*
	 *
	 * Sum all numbers in the array.
	 *
	 * */
	return array.Sum();
    }

    static int SumAsParallel(int[] array)
    {
	/*
	 *
	 * Enable parallelization and then sum.
	 *
	 * */
	return array.AsParallel().Sum();
    }

    static void Main()
    {
	// Generate array.
	int[] array = Enumerable.Range(0, short.MaxValue).ToArray();

	// Test methods.
	Console.WriteLine(SumAsParallel(array));
	Console.WriteLine(SumDefault(array));

	const int m = 10000;
	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    SumDefault(array);
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    SumAsParallel(array);
	}
	s2.Stop();
	Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
	    m).ToString("0.00 ns"));
	Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
	    m).ToString("0.00 ns"));
	Console.Read();
    }
}

Result for 32767 elements

536821761
536821761
232450.53 ns
118515.85 ns

Performance result. We can see that AsParallel makes the query twice as fast. This is the result on a dual-core machine under light load. If you have only one processor core, the results should be close. If you have more than two processor cores, the AsParallel call may increase performance even more.

Slow versus fast queries

Programming tip

AsParallel will not have great results on all kinds of queries. For small collections you run a query on, AsParallel will be slower because of the extra method call. For example, I changed the array to only have two elements with Element.Range(0, 2).

Result with two elements

1
1
   48.84 ns
10914.27 ns

With two elements, AsParallel makes the query run over 200 times slower instead of two times faster. Time to find a new C# programming website to read instead of this one.

Summary

.NET Framework information

It's tempting to think that AsParallel will make queries twice as fast if you call it on a computer with a dual-core processor. This is true on certain, long-running queries, but it will make small and fast queries more than 200 times slower. If you have a query that is computationally intensive, AsParallel might be helpful. Alternatively, if you really need better performance an imperative method implementation might be even better.

LINQ Examples
.NET