Ordered PLINQ ForAll

后端 未结 6 1287
长情又很酷
长情又很酷 2021-01-04 08:09

The msdn documentation about order preservation in PLINQ states the following about ForAll().

  • Result when the source sequence is ordered
6条回答
  •  抹茶落季
    2021-01-04 08:22

    Now as an extension method:

    It will process on multiple cores then will order the results, so there's the overhead of ordering. Here's an answer on benchmarking simple for vs parallel.

     public static IEnumerable OrderedParallel(this IEnumerable list, Func action)
        {
            var unorderedResult = new ConcurrentBag<(long, T1)>();
            Parallel.ForEach(list, (o, state, i) =>
            {
                unorderedResult.Add((i, action.Invoke(o)));
            });
            var ordered = unorderedResult.OrderBy(o => o.Item1);
            return ordered.Select(o => o.Item2);
        }
    

    use like:

    var result = Events.OrderedParallel(eventItem => ...);
    

    Hope this will save you some time.

提交回复
热议问题