PLINQ性能 - 将ParallelQuery转换回List [英] PLINQ performance - converting ParallelQuery back to List

查看:124
本文介绍了PLINQ性能 - 将ParallelQuery转换回List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在'ParallelQuery'上创建了一个扩展方法'Where',它接受谓词(字符串)和值(object [])。该方法如下所示 -

I have created an extension method 'Where' on 'ParallelQuery' that takes the predicate (string) and values (object[]). The method looks like below -

public static List< T>其中< T>(此ParallelQuery< T>源,字符串谓词,params对象[]值)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(source == null)抛出新的ArgumentNullException(" source");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(predicate == null)抛出新的ArgumentNullException(" predicate");



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var expression = DynamicExpression.ParseLambda(typeof(T),typeof(bool),谓词,值);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var whereFunc = expression.Compile();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return ParallelEnumerable.ToList< T>(ParallelEnumerable.Where< T>(source,(dynamic)whereFunc));

  &NBSP; &NBSP; &NBSP; }

public static List<T> Where<T>(this ParallelQuery<T> source, string predicate, params object[] values)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");

            var expression = DynamicExpression.ParseLambda(typeof(T), typeof(bool), predicate, values);
            var whereFunc = expression.Compile();
            return ParallelEnumerable.ToList<T>(ParallelEnumerable.Where<T>(source, (dynamic)whereFunc));
        }

我调用此函数如下 -

I am calling this function as below -

var filteredList = Parties.AsParallel() 。$
WithExecutionMode(ParallelExecutionMode.ForceParallelism)。

WithMergeOptions(ParallelMergeOptions.FullyBuffered)。

其中< PartyMainObject>(谓词,值);

var filteredList = Parties.AsParallel().
WithExecutionMode(ParallelExecutionMode.ForceParallelism).
WithMergeOptions(ParallelMergeOptions.FullyBuffered).
Where<PartyMainObject>(predicate, values);

- 如果缔约方是一个清单< PartyMainObject>。

- Where Parties is a List<PartyMainObject>.

该功能在单个用户中表现良好,并在1ms内完成。但是当我把50个用户加载时,性能在1ms到320ms之间变化,大多数情况下都在100ms以上。

The function performs well in single user and completes in 1ms. But when I put 50 users load on it, the performance varies between 1ms to 320ms, in most cases it is above 100ms.

你能否建议如何提高上述性能方法对50个用户加载?我已经尝试记录每个语句,发现ToList占用了大部分时间。非常感谢早期回复。

Could you please suggest how can I boost the performance of the above method on 50 users load ? I have tried logging each statement and found that ToList is taking majority of the time. An early reply would be much appreciated.

问候,

Akash

推荐答案

您好Akash.Uniyal,

Hi Akash.Uniyal,

感谢您在此发帖。

在许多情况下,并行执行意味着查询运行得更快。但是,并行性可能会引入其自身的复杂性,并非PLINQ中的所有查询操作都运行得更快。实际上,并行化实际上会降低某些查询的速度。因此,
您应该了解排序等问题如何影响并行查询。

In many cases, parallel execution means that the query runs significantly faster. However, parallelism can introduce its own complexities, and not all query operations run faster in PLINQ. In fact, parallelization actually slows down certain queries. Therefore, you should understand how issues such as ordering affect parallel queries.

请参阅MSDN文档,根据您自己的情况加速您的PLINQ。

Please refer to the MSDN document to speed your PLINQ according to your own scenarios.

https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/understanding-speedup- in-plinq

最诚挚的问候,

Wendy


这篇关于PLINQ性能 - 将ParallelQuery转换回List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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