是否在并行LINQ let子句强制并行的评价? [英] Does let clause in parallel linq force the parallel evaluation?

查看:175
本文介绍了是否在并行LINQ let子句强制并行的评价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xamarin iOS的网站有以下并行LINQ的例子。

On the xamarin iOS site there is the following parallel linq example.

from item in items.AsParallel ()
   let result = DoExpensiveWork (item)
   select result;

难道这不是被写入

Could this not have been written

from item in items.AsParallel ()
   select DoExpensiveWork (item);

或者是有什么原因let子句?这是否生效,并行进行评价?

Or is there a reason for the let clause? Does this force it to be evaluated in parallel?

推荐答案

让我们来看看生成的code(反编译,当然):

Let's look at the generated code (decompiled of course):

private static void WithLet(IEnumerable<int> items)
{
  ParallelEnumerable.Select(ParallelEnumerable.Select(ParallelEnumerable.AsParallel<int>(items), item =>
  {
    var local_0 = new
    {
      item = item,
      result = Program.DoExpensiveWork(item)
    };
    return local_0;
  }), param0 => param0.result);
}

private static void WithoutLet(IEnumerable<int> items)
{
  ParallelEnumerable.Select<int, object>(ParallelEnumerable.AsParallel<int>(items), (Func<int, object>) (item => Program.DoExpensiveWork(item)));
}

正如我们可以看到的两种方法之间的唯一区别是,创建一个中间局部变量。分配到此本地变量不改变程序的行为。

As we can see the only difference between the two methods is that an intermediate local variable is created. The assignment to this local variable does not change the program's behaviour.

要更具体回答你的问题了code,强制执行是在平行实际上是调用 ParallelEnumerable.AsParallel 。 LINQ查询仅执行时列举所以按理说,这是发电机的的IEnumerable&LT;&GT; 指定无论是在平行或不执行

To answer your question more specifically the code that forces the execution to be in parallel is actually the call to ParallelEnumerable.AsParallel. LINQ queries are only executed when enumerated so it stands to reason that it is the generator of the IEnumerable<> that specifies whether it is executed in parallel or not.

这篇关于是否在并行LINQ let子句强制并行的评价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆