I have a question.
Are there any advantages to using Parallel.Invoke inside another Parallel.ForEach?
Here is my code:
Parallel.ForEach(yearMonths, () => new List<DJVSStatsCo>(), (yearMonth, loopState, localDjvsStatsCo) => { var coVintageCounter = 0; var coExitsCounter = 0; var coExtant = 0; Parallel.Invoke(() => coVintageCounter = globalData.ValuationEventsPit. Where(x => x.FirstRoundYearMonth <= yearMonth). Select(x => x.CompanyId).Distinct().Count(), () => coExitsCounter = globalData.ValuationEventsPit. Where(x => x.ExitDate != null && x.ExitDateYearMonth == yearMonth). Select(x => x.CompanyId).Distinct().Count(), () => coExtant = globalData.ValuationEventsPit. Where(x => x.FirstRoundYearMonth <= yearMonth && (x.ExitDate == null || x.ExitDateYearMonth > yearMonth)). Select(x => x.CompanyId).Distinct().Count() ); localDjvsStatsCo.Add(new DJVSStatsCo(yearMonth, coVintageCounter, coExtant, coExitsCounter)); return localDjvsStatsCo; }, x => { lock (locker) { djvsStatsCos.AddRange(x); } });
I have about 50 thousand records, and my machine has 2 main processors and calculates the calculation time. I get almost the same result. So my question is, are there any advantages to using Parallel inside Parallel? What is the best practice for this?
Thank you very much.
Regards, Vlad.
source share