상속: IDisposable
예제 #1
0
        private static void ParallelAggregationWithTpl()
        {
            using (ExecutionTimer.Start("Parallel.ForEach", blockResults: true))
            {
                var parallelResults     = new List <NamespaceItem>();
                var parallelResultsLock = new object();
                Parallel.ForEach(
                    // the IEnumerable to enumerate over...
                    FilesToRead,

                    // initialization
                    () => new List <NamespaceItem>(),

                    // loop body
                    (line, loopState, partialResult) =>
                {
                    partialResult.Add(ParseText(File.ReadAllText(line)));
                    return(partialResult);
                },

                    // local finally (good for aggregation)
                    (localPartialResult) =>
                {
                    lock (parallelResultsLock)
                    {
                        parallelResults.AddRange(localPartialResult);
                    }
                });

                Console.WriteLine("Results: {0}", parallelResults.Count);
            }
        }
예제 #2
0
 private static void ParallelAggregationWithLinq()
 {
     using (ExecutionTimer.Start("PLINQ (AsParallel)", blockResults: true))
     {
         var linqResults = FilesToRead
                           .AsParallel()
                           .Select(x => ParseText(File.ReadAllText(x)))
                           .ToList();
         Console.WriteLine("Results: {0}", linqResults.Count);
     }
 }
예제 #3
0
 private static void SynchronousAggregation()
 {
     using (ExecutionTimer.Start("SynchronousAggregation", blockResults: true))
     {
         var syncResults = new List <NamespaceItem>();
         foreach (var line in FilesToRead)
         {
             syncResults.Add(ParseText(File.ReadAllText(line)));
         }
         Console.WriteLine("Results: {0}", syncResults.Count);
     }
 }
예제 #4
0
        static void Main(string[] args)
        {
            ExecutionTimer.Options(
                showInfo: true
                );

            SynchronousAggregation();

            ParallelAggregationWithTpl();

            ParallelAggregationWithLinq();
        }