Share via


Gewusst wie: Schreiben einer benutzerdefinierten PLINQ-Aggregatfunktion

In diesem Beispiel wird gezeigt, wie Sie mit der Aggregate-Methode eine benutzerdefinierte Aggregatfunktion auf eine Quellsequenz anwenden.

WarnhinweisVorsicht

Dieses Beispiel soll die Verwendung veranschaulichen, und es wird möglicherweise nicht schneller als die entsprechende sequenzielle LINQ to Objects-Abfrage ausgeführt.Weitere Informationen über Geschwindigkeitssteigerungen finden Sie unter Grundlagen zur Beschleunigung in PLINQ.

Beispiel

Im folgenden Beispiel wird die Standardabweichung einer Sequenz von Ganzzahlen berechnet.

Class aggregation
    Private Shared Sub Main(ByVal args As String())

        ' Create a data source for demonstration purposes.
        Dim source As Integer() = New Integer(99999) {}
        Dim rand As New Random()
        For x As Integer = 0 To source.Length - 1
            ' Should result in a mean of approximately 15.0.
            source(x) = rand.[Next](10, 20)
        Next

        ' Standard deviation calculation requires that we first
        ' calculate the mean average. Average is a predefined
        ' aggregation operator, along with Max, Min and Count.
        Dim mean As Double = source.AsParallel().Average()


        ' We use the overload that is unique to ParallelEnumerable. The 
        ' third Func parameter combines the results from each thread.
        ' initialize subtotal. Use decimal point to tell 
        ' the compiler this is a type double. Can also use: 0d.

        ' do this on each thread

        ' aggregate results after all threads are done.

        ' perform standard deviation calc on the aggregated result.
        Dim standardDev As Double = source.AsParallel().Aggregate(0.0R, Function(subtotal, item) subtotal + Math.Pow((item - mean), 2), Function(total, thisThread) total + thisThread, Function(finalSum) Math.Sqrt((finalSum / (source.Length - 1))))
        Console.WriteLine("Mean value is = {0}", mean)
        Console.WriteLine("Standard deviation is {0}", standardDev)

        Console.ReadLine()
    End Sub
End Class
namespace PLINQAggregation
{
    using System;
    using System.Linq;

    class aggregation
    {
        static void Main(string[] args)
        {

            // Create a data source for demonstration purposes.
            int[] source = new int[100000];
            Random rand = new Random();
            for (int x = 0; x < source.Length; x++)
            {
                // Should result in a mean of approximately 15.0.
                source[x] = rand.Next(10, 20);
            }

            // Standard deviation calculation requires that we first
            // calculate the mean average. Average is a predefined
            // aggregation operator, along with Max, Min and Count.
            double mean = source.AsParallel().Average();


            // We use the overload that is unique to ParallelEnumerable. The 
            // third Func parameter combines the results from each thread.
            double standardDev = source.AsParallel().Aggregate(
                // initialize subtotal. Use decimal point to tell 
                // the compiler this is a type double. Can also use: 0d.
                0.0,

                // do this on each thread
                 (subtotal, item) => subtotal + Math.Pow((item - mean), 2),

                 // aggregate results after all threads are done.
                 (total, thisThread) => total + thisThread,

                // perform standard deviation calc on the aggregated result.
                (finalSum) => Math.Sqrt((finalSum / (source.Length - 1)))
            );
            Console.WriteLine("Mean value is = {0}", mean);
            Console.WriteLine("Standard deviation is {0}", standardDev);
            Console.ReadLine();

        }
    }
}

Dieses Beispiel verwendet eine Überladung des standardmäßigen Aggregatabfrageoperators, die für PLINQ eindeutig ist. Diese Überladung unterstützt eine zusätzliche System.Func<T1, T2, TResult> als dritten Eingabeparameter. Der Delegat kombiniert die Ergebnisse aus allen Threads, bevor die abschließende Berechnung der aggregierten Ergebnissen durchgeführt wird. In diesem Beispiel werden die Summen aus allen Threads addiert.

Wenn der Textkörper eines Lambda-Ausdrucks aus einem einzelnen Ausdruck besteht, entspricht der Rückgabewert des System.Func<T, TResult>-Delegaten dem Wert des Ausdrucks.

Siehe auch

Referenz

ParallelEnumerable

Konzepte

Paralleles LINQ (PLINQ)

Änderungsprotokoll

Datum

Versionsgeschichte

Grund

Mai 2010

Hinweis bezüglich Verwendung und Geschwindigkeitssteigerung hinzugefügt.

Kundenfeedback.