LINQ on HashSet 与 List 的对比
Where LINQ on HashSet vs. List
我需要计算具有给定值的 属性 的 list/set 的元素。列表很大,我需要尽可能好的性能。我应该使用列表还是集合(当有独特的元素时)?有没有更快的方法?
int counter = myList.Where(x => x.A == myValue || x.B == myValue).Count()
这已经在 AsParallel().ForAll()
的另一个大列表中。不,我无法改变这一点。
编辑
我已经看到 this question 并且它绝对不能解决我的问题,我对 (P)LINQ 查询的差异很感兴趣。
如果您遍历整个集合,遍历整个列表可能会比遍历整个集合产生更好的性能,因为列表元素在内存中的分配方式(假设您使用 List<T>
, 不是链表).
如果您对 myList
中的相同数据执行数千个此类查询,您可以通过在 x.A
、[=15= 上构建三个 look-up 表来提高性能], 以及 x.A == x.B
:
时的共同值
var countByA = myList
.GroupBy(x => x.A)
.ToDictionary(g => g.Key, g => g.Count());
var countByB = myList
.GroupBy(x => x.B)
.ToDictionary(g => g.Key, g => g.Count());
var countByAandB = myList
.Where(x => x.A == x.B)
.GroupBy(x => x.A)
.ToDictionary(g => g.Key, g => g.Count());
现在可以使用 inclusion-exclusion principle:
将您的查询转换为三个 look-up
countByA.TryGetValue(myValue, out var counterA);
countByB.TryGetValue(myValue, out var counterB);
countByAandB.TryGetValue(myValue, out var counterAandB);
int counter = counterA + counterB - counterAandB;
我需要计算具有给定值的 属性 的 list/set 的元素。列表很大,我需要尽可能好的性能。我应该使用列表还是集合(当有独特的元素时)?有没有更快的方法?
int counter = myList.Where(x => x.A == myValue || x.B == myValue).Count()
这已经在 AsParallel().ForAll()
的另一个大列表中。不,我无法改变这一点。
编辑
我已经看到 this question 并且它绝对不能解决我的问题,我对 (P)LINQ 查询的差异很感兴趣。
如果您遍历整个集合,遍历整个列表可能会比遍历整个集合产生更好的性能,因为列表元素在内存中的分配方式(假设您使用 List<T>
, 不是链表).
如果您对 myList
中的相同数据执行数千个此类查询,您可以通过在 x.A
、[=15= 上构建三个 look-up 表来提高性能], 以及 x.A == x.B
:
var countByA = myList
.GroupBy(x => x.A)
.ToDictionary(g => g.Key, g => g.Count());
var countByB = myList
.GroupBy(x => x.B)
.ToDictionary(g => g.Key, g => g.Count());
var countByAandB = myList
.Where(x => x.A == x.B)
.GroupBy(x => x.A)
.ToDictionary(g => g.Key, g => g.Count());
现在可以使用 inclusion-exclusion principle:
将您的查询转换为三个 look-upcountByA.TryGetValue(myValue, out var counterA);
countByB.TryGetValue(myValue, out var counterB);
countByAandB.TryGetValue(myValue, out var counterAandB);
int counter = counterA + counterB - counterAandB;