嵌套并行查询
运行嵌套 PLINQ 查询是否存在任何问题?
例如:
//Contains roughly 7000+ elements
mycollections.AsParallel().ForAll(x => {
//contains 12 elements
anothercollection.AsParallel().ForAll(y => {
//download some data from the web and parse it
});
});
Are there any issues with running nested PLINQ queries?
For instance:
//Contains roughly 7000+ elements
mycollections.AsParallel().ForAll(x => {
//contains 12 elements
anothercollection.AsParallel().ForAll(y => {
//download some data from the web and parse it
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用嵌套查询不存在根本问题,因此您当然可以这样做,并且 PLINQ 将尽力尽可能高效地并行化代码。然而,这可能是一个需要考虑的事情,如果您想获得最佳性能,您绝对应该运行一些测量。
最佳选择取决于两个集合中的元素数量以及运行处理所需的时间。
如果外部集合包含大量数字元素,那么内部循环可能是不必要的,因为单个外部循环足以为 PLINQ 提供足够的并行空间。事实上,内部循环可能只会增加开销(不过,根据我的经验,这种情况只发生在大量元素的情况下)
此外,如果您只想并行化单个循环,那么它应该是外部循环。这样,所有工作都可以一次拆分,并且您只需承担一次并行化开销。
There are no fundamental issues with using nested queries, so you can certainly do that and PLINQ will try to do its best to parallelize the code as efficiently as possible. However, it may be a thing to consider and you should definitely run some measurements if you want to get the best possible performance.
The best option depends on the number of elements in both of the collections and the time needed to run the processing.
If the outer collection is small enough, then you'll need the inner loop too, so that you create enough potential for parallelisation (generate enough tasks for PLINQ, so that it can ballance the execution)
If the outer collection contains a large number of elements, then the inner loop is likely unnecessary, because the single outer loop is enough to give PLINQ enough space for parallelization. In fact, the inner loop may be only adding overhead (though, from my experience, this happens only with a very large number of elements)
Also, if you're going to parallelize only a single loop, it should be the outer one. This way all work can be split at once and you'll incur the parallelization overhead only once.