C# provides a powerful set of algorithms for standard containers, such as array and List, when you add “using System.Linq;” statement to your code. Arrays and List containers gain such functionality as Sort, Equal, Sum, Union, Where, Min, Max, First, Last, and so on. For instance, these make it simple to find the minimum element within an array of integers, or the maximum, or the array sum. It’s even possible to find the minimum element within an array of your own objects, by providing your own comparison method. Array and List are generic containers, able to hold any data type: built-in type or user created classes/types.
Most of these algorithms benefit from parallel processing, especially if arrays or lists are fairly large. I’ve developed a library of high performance versions of the above algorithms. This library performs the same functions, using more efficient sequential techniques and parallel algorithms utilizing multiple processor cores simultaneously. So far, performance gains have been encouraging and dramatic.
For instance, SequenceEqual is a very useful operation when testing: for comparison of two arrays or Lists for equality. This function returns true when two arrays are precisely equal, element by element; or, returns false when one or more array elements are not equal. The following graph shows performance gains from the parallel version for arrays of integers:
The horizontal axis is the array size, varying up to 100 Million. The vertical axis is the speedup provided by the parallel version over the C# standard version. The speedup on my quad-core i7 laptop is between 4-11X. Instead of waiting for 1.5 seconds when comparing two 100 Million element integer arrays, the parallel comparison takes only 0.2 seconds.
Look for this library of High Performance C# algorithms shortly on https://www.nuget.org . It will be called HPCsharp. It will start with a few accelerated algorithms, with more to be added over time.
Parallel Linq (PLinq)
C# also provides parallel versions of some of these algorithms in the Parallel Linq (PLinq) package. It provides a wonderful abstraction at the price of performance and capacity, as we will see with benchmarks (coming soon…)