C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: AsParallel can make certain queries much faster, and other queries much slower.
Note 2: Performance depends on the target machine and the characteristics of the query and your data source.
Result: We can see that AsParallel makes the query twice as fast. This is the result on a dual-core machine under light load.
Tip: If you have more than two processor cores, the AsParallel call may increase performance even more.
C# program that uses AsParallel
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
Result: With two elements, AsParallel makes the query run over 200 times slower instead of two times faster.
Result with two elements
1
1
48.84 ns
10914.27 ns
However: The AsParallel call causes multiple threads to access the static field. The threads use IsEvenCounter, a non-thread-safe method.
StaticSo: The results are unpredictable. In the array with 500 even numbers, this program will count 478, 500, or 517 even numbers.
Odd, EvenNote: If you remove the AsParallel() call from the statement, the result is always 500.
Tip: This program proves that AsParallel is actually creating multiple threads. And this exposes an error in the program design.
C# program that shows AsParallel problem
using System;
using System.Linq;
class Program
{
static int _counter;
static bool IsEvenCounter(int value)
{
// ... Return whether counter field is even.
return Program._counter++ % 2 == 0;
}
static void Main()
{
Func<int, bool> func = Program.IsEvenCounter;
int[] array = Enumerable.Range(0, 1000).ToArray();
// ... Use parallel query ten times on the array.
// ... Write number of results matched by Func.
for (int i = 0; i < 10; i++)
{
var result = array.AsParallel().Where(func);
Console.WriteLine(result.Count());
}
}
}
Output
517
514
500
509
500
478
500
508
500
501
Note: If you have a query that is computationally intensive, AsParallel might be helpful.
Tip: If you really need better performance an imperative method implementation, with for, might be even better.
For