嵌套并行查询

发布于 2024-09-26 06:39:49 字数 285 浏览 2 评论 0原文

运行嵌套 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 技术交流群。

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

发布评论

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

评论(1

月亮是我掰弯的 2024-10-03 06:39:49

使用嵌套查询不存在根本问题,因此您当然可以这样做,并且 PLINQ 将尽力尽可能高效地并行化代码。然而,这可能是一个需要考虑的事情,如果您想获得最佳性能,您绝对应该运行一些测量。

最佳选择取决于两个集合中的元素数量以及运行处理所需的时间。

  • 外部集合足够小,那么您也需要内部循环,以便为并行化创造足够的潜力(为 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.

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