如何:在 PLINQ 中指定合併選項How to: Specify Merge Options in PLINQ

此範例示範如何指定將套用到 PLINQ 查詢中所有後續運算子的合併選項。This example shows how to specify the merge options that will apply to all subsequent operators in a PLINQ query. 您不需明確地設定合併選項,但這樣做可改善效能。You do not have to set merge options explicitly, but doing so may improve performance. 如需合併選項的詳細資訊,請參閱 PLINQ 中的合併選項For more information about merge options, see Merge Options in PLINQ.

警告

這個範例是為了示範用法,執行速度可能比不上對應的循序 LINQ to Objects 查詢。This example is intended to demonstrate usage, and might not run faster than the equivalent sequential LINQ to Objects query. 如需加速的詳細資訊,請參閱認識 PLINQ 中的加速For more information about speedup, see Understanding Speedup in PLINQ.

範例Example

下列範例示範具有未排序來源之基本案例中的合併選項行為,並將高度耗費資源的函式套用至每個元素。The following example demonstrates the behavior of merge options in a basic scenario that has an unordered source and applies an expensive function to every element.

namespace MergeOptions
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading;

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

            var nums = Enumerable.Range(1, 10000);

            // Replace NotBuffered with AutoBuffered 
            // or FullyBuffered to compare behavior.
            var scanLines = from n in nums.AsParallel()
                                .WithMergeOptions(ParallelMergeOptions.NotBuffered)
                            where n % 2 == 0
                            select ExpensiveFunc(n);

            Stopwatch sw = Stopwatch.StartNew();
            foreach (var line in scanLines)
            {
                Console.WriteLine(line);
            }

            Console.WriteLine("Elapsed time: {0} ms. Press any key to exit.",
                            sw.ElapsedMilliseconds);
            Console.ReadKey();
        }

        // A function that demonstrates what a fly
        // sees when it watches television :-)
        static string ExpensiveFunc(int i)
        {
            Thread.SpinWait(2000000);
            return String.Format("{0} *****************************************", i);
        }
    }
}
Class MergeOptions2


    Sub DoMergeOptions()

        Dim nums = Enumerable.Range(1, 10000)

        ' Replace NotBuffered with AutoBuffered 
        ' or FullyBuffered to compare behavior.
        Dim scanLines = From n In nums.AsParallel().WithMergeOptions(ParallelMergeOptions.NotBuffered)
              Where n Mod 2 = 0
              Select ExpensiveFunc(n)

        Dim sw = System.Diagnostics.Stopwatch.StartNew()
        For Each line In scanLines
            Console.WriteLine(line)
        Next

        Console.WriteLine("Elapsed time: {0} ms. Press any key to exit.")
        Console.ReadKey()

    End Sub
    ' A function that demonstrates what a fly
    ' sees when it watches television :-)
    Function ExpensiveFunc(ByVal i As Integer) As String
        System.Threading.Thread.SpinWait(2000000)
        Return String.Format("{0} *****************************************", i)
    End Function
End Class

如果 AutoBuffered 在產生第一個元素前造成非預期的延遲,請試用 NotBuffered 選項,以更快且更順暢的方式產生結果元素。In cases where the AutoBuffered option incurs an undesirable latency before the first element is yielded, try the NotBuffered option to yield result elements faster and more smoothly.

請參閱See also