IEnumerable 收益率与 .AsParallel() 结合使用

发布于 2024-09-08 10:30:37 字数 591 浏览 11 评论 0原文

我已经编写了一些代码来尝试描述我的担忧:

static void Main(string[] args)
{
    IEnumerable<decimal> marks = GetClassMarks();
    IEnumerable<Person> students = GetStudents();

    students.AsParallel().ForAll(p => GenerateClassReport(p, marks));

    Console.ReadKey();
}

GetClassMarks 使用来自我奇怪的数据源的收益返回。假设GenerateClassReport基本上执行marks.Sum()/marks.Count()来获取班级平均值。

据我了解,students.AsParallel().ForAll 是一个并行的 foreach。

我担心 GetClassMarks 方法内部会发生什么。

  • 是要枚举一次还是多次?
  • 枚举将以什么顺序发生?
  • 我是否需要对标记执行 .ToList() 以确保它只被命中一次?

I've written some code to try and describe my concern:

static void Main(string[] args)
{
    IEnumerable<decimal> marks = GetClassMarks();
    IEnumerable<Person> students = GetStudents();

    students.AsParallel().ForAll(p => GenerateClassReport(p, marks));

    Console.ReadKey();
}

GetClassMarks uses yield return in it from my weird data source. Assume that GenerateClassReport does basically a marks.Sum()/marks.Count() to get the class average.

From what I understand, students.AsParallel().ForAll is a parallel foreach.

My worry is what is going to happen inside the GetClassMarks method.

  • Is it going to be enumerated once or many times?
  • What order is the enumeration going to happen in?
  • Do I need to do a .ToList() on marks to make sure it is only hit once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯离梦残月幽梦 2024-09-15 10:30:37

它会被枚举一次还是多次?

假设 GenerateClassReport() 枚举 marks 一次,那么 marks 将为 students 中的每个元素枚举一次。

枚举将以什么顺序发生?

每个线程都将以其默认顺序枚举集合,但多个线程将同时执行此操作。并发枚举顺序通常是不可预测的。另外,您应该注意,线程的数量是有限的且可变的,因此很可能并非所有枚举都会同时发生。

我是否需要对标记执行 .ToList() 以确保它只被命中一次?

如果 GetClassMarks() 是一个迭代器(即它使用 yield 构造),则其执行将被推迟,并且每次 marks< 都会调用一次。 /code> 被枚举(即 students 中的每个元素都枚举一次)。如果您使用 IEnumerable; marks = GetClassMarks().ToList() 或者如果 GetClassMarks() 内部返回一个具体列表或数组,则 GetClassMarks() 将立即执行,并且结果将在每个并行线程中存储和枚举,而无需再次调用 GetClassMarks()

Is it going to be enumerated once or many times?

Assuming that GenerateClassReport() enumerates marks once, then marks will be enumerated once for each element in students.

What order is the enumeration going to happen in?

Each thread will enumerate the collection in its default order, but several threads will do so concurrently. The concurrent enumeration order is generally unpredictable. Also, you should note that the number of threads is limited and variable, so most likely not all of the enumerations will occur concurrently.

Do I need to do a .ToList() on marks to make sure it is only hit once?

If GetClassMarks() is an iterator (i.e. it uses the yield construct), then its execution will be deferred and it will be called once for each time marks is enumerated (i.e. once for each element in students). If you use IEnumerable<decimal> marks = GetClassMarks().ToList() or if GetClassMarks() internally returns a concrete list or array, then GetClassMarks() will be executed immediately and the results will be stored and enumerated in each of the parallel threads without calling GetClassMarks() again.

长伴 2024-09-15 10:30:37
  1. 如果GetClassMarks是一个迭代器——也就是说,如果它在内部使用yield——那么它实际上是一个查询,每当您调用< code>marks.Sum()、marks.Count()

  2. 几乎不可能预测并行查询中的执行顺序。

  3. 是的。以下内容将确保 GetClassMarks 仅执行一次。后续调用 marks.Sum()marks.Count() 等将使用具体列表,而不是重新执行 GetClassMarks 查询。

    列表<十进制>标记 = GetClassMarks().ToList();
    

请注意,无论您是否使用 AsParallel13 点都适用。在这两种情况下,GetClassMarks 查询将被执行完全相同的次数(假设代码的其余部分(除了并行方面之外)是相同的)。

  1. If GetClassMarks is an iterator -- that is, if it uses yield internally -- then it is effectively a query that will be re-executed whenever you call marks.Sum(), marks.Count() etc.

  2. It's almost impossible to predict the order of execution in a parallel query.

  3. Yes. The following will ensure that GetClassMarks is only executed once. Subsequent calls to marks.Sum(), marks.Count() etc will use the concrete list rather than re-executing the GetClassMarks query.

    List<decimal> marks = GetClassMarks().ToList();
    

Note that points 1 and 3 apply whether or not you're using AsParallel. The GetClassMarks query will be executed exactly the same number of times in either case (assuming that the rest of the code, except for the parallel aspects, is the same).

ι不睡觉的鱼゛ 2024-09-15 10:30:37

它会被枚举一次还是多次?

就一次。

枚举将以什么顺序发生?

迭代器(使用yield的函数)确定顺序。

我是否需要对标记执行 .ToList() 以确保它只被命中一次?

不会。

AsParallel 仅迭代其输入一次,将输入划分为分派给工作线程的块。

Is it going to be enumerated once or many times?

Just once.

What order is the enumeration going to happen in?

The iterator (function using yield) determines the order.

Do I need to do a .ToList() on marks to make sure it is only hit once?

No.

AsParallel only iterates through its input once, partitioning the input into blocks which are dispatched to worker threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文