gpt4 book ai didi

c# - AsParallel - Linq Where 子句性能之前与之后

转载 作者:行者123 更新时间:2023-11-30 19:55:45 25 4
gpt4 key购买 nike

我有一个 List<Boss>收藏,每个Boss都有2到10个助理职员。我正在对包括老板在内的所有员工进行分组。现在我有 List<Person> ,由此我正在使用 Parallel LINQ 搜索 "Raj",我可以在哪里放置支持方法 AsParallel()在 Where 子句之前或之后获得更好的性能?

public class Person
{
public int EmpID { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public string Gender { get; set; }
}

void Main()
{

List<Boss> BossList = new List<Boss>()
{
new Boss()
{
EmpID = 101,
Name = "Harry",
Department = "Development",
Gender = "Male",
Employees = new List<Person>()
{
new Person() {EmpID = 102, Name = "Peter", Department = "Development",Gender = "Male"},
new Person() {EmpID = 103, Name = "Emma Watson", Department = "Development",Gender = "Female"},

}
},
new Boss()
{
EmpID = 104,
Name = "Raj",
Department = "Development",
Gender = "Male",
Employees = new List<Person>()
{
new Person() {EmpID = 105, Name = "Kaliya", Department = "Development",Gender = "Male"},
new Person() {EmpID = 103, Name = "Emma Watson", Department = "Development",Gender = "Female"},

}
}
};

List<Person> result = BossList
.SelectMany(x =>
new[] { new Person { Name = x.Name, Department = x.Department, Gender = x.Gender, EmpID = x.EmpID } }
.Concat(x.Employees))
.GroupBy(x => x.EmpID) //Group by employee ID
.Select(g => g.First()) //And select a single instance for each unique employee
.ToList();

List<Person> SelectedResult = new List<Person>();

// AsParallel() - Before Where Clause
SelectedResult = result.AsParallel().Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).ToList();


// AsParallel() - After Where Clause
SelectedResult = result.Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).AsParallel().ToList();
}

核心源代码:

    List<Person> SelectedResult = new List<Person>();

// AsParallel() - Before Where Clause
SelectedResult = result.AsParallel().Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).ToList();


// AsParallel() - After Where Clause
SelectedResult = result.Where(m => m.Name.ToLowerInvariant().Contains("Raj".ToLowerInvariant())).AsParallel().ToList();

最佳答案

之前。

AsParallel 帮助我们并行运行查询,这使并行线程能够提高性能。如果您将 WHERE 子句放在前面,过滤将按顺序完成,然后才会并行化。

这是一些测试代码:

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;

class AsParallelTest
{
static void Main()
{
var query = Enumerable.Range(0, 1000)
.Select(ProjectionExample)
.Where(x => x > 10)
.AsParallel();

Stopwatch stopWatch = Stopwatch.StartNew();
int count = query.Count();
stopWatch.Stop();

Console.WriteLine("Count: {0} in {1}ms", count,
stopWatch.ElapsedMilliseconds);

query = Enumerable.Range(0, 1000)
.AsParallel()
.Select(ProjectionExample)
.Where(x => x > 10);

stopWatch = Stopwatch.StartNew();
count = query.Count();
stopWatch.Stop();

Console.WriteLine("Count: {0} in {1}ms", count,
stopWatch.ElapsedMilliseconds);
}

static int ProjectionExample(int arg)
{
Thread.Sleep(10);
return arg;
}
}

结果:

Count: 989 in 10574ms
Count: 989 in 1409ms

很明显,第一个结果没有并行化,而第二个结果并行化了。如果您只有一个处理器核心,结果应该很接近。如果您有两个以上的处理器内核,则 AsParallel 调用可能会进一步提高性能。另外,您可以阅读 this文章。

关于c# - AsParallel - Linq Where 子句性能之前与之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com