Inheritance: IDisposable
Beispiel #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);
            }
        }
Beispiel #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);
     }
 }
Beispiel #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);
     }
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            ExecutionTimer.Options(
                showInfo: true
                );

            SynchronousAggregation();

            ParallelAggregationWithTpl();

            ParallelAggregationWithLinq();
        }