简体   繁体   English

PLinq本质上比System.Threading.Tasks.Parallel.ForEach快

[英]Is PLinq Inherently Faster than System.Threading.Tasks.Parallel.ForEach

Summary : I changed from System.Threading.Tasks.Parallel.ForEach and Concurrent Data structure to a simple plinq (Parallel Linq) query. 摘要 :我从System.Threading.Tasks.Parallel.ForEach和并发数据结构更改为简单的plinq(并行Linq)查询。 The speed up was amazing . 速度惊人

So is plinq inherently faster than Parallel.ForEach? 那么plinq本质上比Parallel.ForEach快吗? Or is it specific to the task. 还是特定于任务。

// Original Code
// concurrent dictionary to store results
var resultDict = new ConcurrentDictionary<string, MyResultType>();

Parallel.ForEach(items, item =>
        {
            resultDict.TryAdd(item.Name, PerformWork(source));
        });


// new code

var results =
            items
            .AsParallel()
            .Select(item => new { item.Name, queryResult = PerformWork(item) })
            .ToDictionary(kv => kv.SourceName, kv => kv.queryResult);

Notes : Each task (PerformWork) now runs between 0 and 200 ms. 注意 :每个任务(PerformWork)现在在0到200毫秒之间运行。 It used to take longer before I optimized it. 在我对其进行优化之前,它花费了更长的时间。 That's why I was using the Tasks.Parallel library in the fist place. 这就是为什么我首先使用Tasks.Parallel库的原因。 So I went from 2 seconds total time to ~100-200 ms total time, performing roughly the same work, just with different methods. 因此,我将总时间从2秒增加到了100-200 ms,使用不同的方法执行大致相同的工作。 (Wow linq and plinq are awesome!) (哇,linq和plinq很棒!)

Questions : 问题

  1. Is the speed up due to using plinq vs Parallel.ForEach? 是由于使用plinq vs Parallel.ForEach而加快了速度吗?
  2. Is it instead simply the removal of the concurrent data structure (ConcurrentDictionary)? 而是简单地删除并发数据结构(ConcurrentDictionary)? (Because it doesn't need to synchronize threads). (因为它不需要同步线程)。
  3. Based on the answer from this related question 根据相关问题的答案

Whereas PLINQ is largely based on a functional style of programming with no side-effects, side-effects are precisely what the TPL is for. 尽管PLINQ主要基于一种没有副作用的编程功能样式,但副作用正是TPL的目的。 If you want to actually do work in parallel as opposed to just searching/selecting things in parallel, you use the TPL. 如果您实际上想并行工作而不是并行搜索/选择事物,则可以使用TPL。

Can I assume that because my pattern is basically functional (giving inputs produce new outputs without mutation), that plinq is the correct technology to use? 我是否可以假设由于我的模式基本上是可以正常工作的(给出的输入会产生没有突变的新输出),所以plinq是使用的正确技术吗?

I'm looking for validation that my assumptions are correct, or an indication that I'm missing something. 我正在寻找验证我的假设正确的证据,或者表明我缺少某些东西。

It's not possible to use these 2 code samples to do a definitive comparison between Parallel.ForEach and PLINQ. 不可能使用这两个代码样本在Parallel.ForEach和PLINQ之间进行确定的比较。 The code samples are simply too different. 代码示例完全不同。

The first item that jumps out at me is the first sample uses ConcurrentDictionary and the second uses Dictionary . 第一个让我惊讶的项目是第一个示例使用ConcurrentDictionary ,第二个示例使用Dictionary These two types have very different uses and performance characteristics. 这两种类型具有非常不同的用途和性能特征。 In order to get an accurate comparison between the two technologies you need to be consistent here with the types. 为了在两种技术之间进行准确的比较,您需要在此处与类型保持一致。

Based on the limited information you've provided in your sample (I asked for more details in a comment on the OP), I'm guessing sure you're seeing differences due to the partitioning algorithm that is used. 根据您在示例中提供的有限信息(我在OP的评论中要求提供更多详细信息),我猜想您肯定会因为使用的分区算法而看到差异。 You should read up on Chunk Partitioning vs. Range Partitioning in this blog post where he discusses how they differ and for which types of work they might be best suited for. 您应该在此博客文章中了解“ 块分区与范围分区”,在此他将讨论它们之间的差异以及它们最适合的工作类型。 Highly recommend you read that blog article as well as this one which goes into a little more detail on those two types along with two other types of partitioning that can be used, though not applicable to your sample, as well as giving some visual aids to better understand the partitioning. 强烈建议您阅读该博客文章以及文章,其中将详细介绍这两种类型以及可以使用的两种其他类型的分区(尽管不适用于您的示例),并提供一些视觉帮助。更好地了解分区。 Finally, here's yet another blog post that discusses work partitioning and how it can affect you when the default partitioning algorithm doesn't make sense for your particular workload. 最后, 这是另一篇博客文章 ,讨论工作分区以及当默认分区算法对您的特定工作负载没有意义时,工作分区如何影响您。 That post actually refers to a great program that helps you visualize the partitioners at work that's part of a set of parallel samples from the PFX team . 该帖子实际上是一个很棒的程序,可以帮助您可视化工作中的分区程序,这些程序是PFX团队提供一组并行样本的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM