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); } }
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); } }
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); } }
static void Main(string[] args) { ExecutionTimer.Options( showInfo: true ); SynchronousAggregation(); ParallelAggregationWithTpl(); ParallelAggregationWithLinq(); }