IEnumerable 收益率与 .AsParallel() 结合使用
我已经编写了一些代码来尝试描述我的担忧:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设
GenerateClassReport()
枚举marks
一次,那么marks
将为students
中的每个元素枚举一次。每个线程都将以其默认顺序枚举集合,但多个线程将同时执行此操作。并发枚举顺序通常是不可预测的。另外,您应该注意,线程的数量是有限的且可变的,因此很可能并非所有枚举都会同时发生。
如果
GetClassMarks()
是一个迭代器(即它使用yield
构造),则其执行将被推迟,并且每次marks< 都会调用一次。 /code> 被枚举(即
students
中的每个元素都枚举一次)。如果您使用IEnumerable; marks = GetClassMarks().ToList()
或者如果GetClassMarks()
内部返回一个具体列表或数组,则GetClassMarks()
将立即执行,并且结果将在每个并行线程中存储和枚举,而无需再次调用GetClassMarks()
。Assuming that
GenerateClassReport()
enumeratesmarks
once, thenmarks
will be enumerated once for each element instudents
.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.
If
GetClassMarks()
is an iterator (i.e. it uses theyield
construct), then its execution will be deferred and it will be called once for each timemarks
is enumerated (i.e. once for each element instudents
). If you useIEnumerable<decimal> marks = GetClassMarks().ToList()
or ifGetClassMarks()
internally returns a concrete list or array, thenGetClassMarks()
will be executed immediately and the results will be stored and enumerated in each of the parallel threads without callingGetClassMarks()
again.如果
GetClassMarks
是一个迭代器——也就是说,如果它在内部使用yield
——那么它实际上是一个查询,每当您调用< code>marks.Sum()、marks.Count()
等几乎不可能预测并行查询中的执行顺序。
是的。以下内容将确保
GetClassMarks
仅执行一次。后续调用marks.Sum()
、marks.Count()
等将使用具体列表,而不是重新执行GetClassMarks
查询。请注意,无论您是否使用
AsParallel
,1 和 3 点都适用。在这两种情况下,GetClassMarks
查询将被执行完全相同的次数(假设代码的其余部分(除了并行方面之外)是相同的)。If
GetClassMarks
is an iterator -- that is, if it usesyield
internally -- then it is effectively a query that will be re-executed whenever you callmarks.Sum()
,marks.Count()
etc.It's almost impossible to predict the order of execution in a parallel query.
Yes. The following will ensure that
GetClassMarks
is only executed once. Subsequent calls tomarks.Sum()
,marks.Count()
etc will use the concrete list rather than re-executing theGetClassMarks
query.Note that points 1 and 3 apply whether or not you're using
AsParallel
. TheGetClassMarks
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).就一次。
迭代器(使用
yield
的函数)确定顺序。不会。
AsParallel
仅迭代其输入一次,将输入划分为分派给工作线程的块。Just once.
The iterator (function using
yield
) determines the order.No.
AsParallel
only iterates through its input once, partitioning the input into blocks which are dispatched to worker threads.