两个字符串数组的快速计数()路口 [英] Fast count() intersection of two string arrays
本文介绍了两个字符串数组的快速计数()路口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要计算对应的字符串两个大数组的交集元素的数量并做到这一点非常快。
I need to count the number of elements corresponding to the intersection of two big arrays of strings and do it very fast.
我使用下面的代码:
arr1[i].Intersect(arr2[j]).Count()
有关CPU时间,VS探查表明
For CPU Time, VS Profiler indicates
- 85.1的%
System.Linq.Enumerable.Count()
- 0.3
System.Linq.Enumerable.Intersect%( )
- 85.1% in
System.Linq.Enumerable.Count()
- 0.3% in
System.Linq.Enumerable.Intersect()
不幸的是它可能需要几个小时来完成所有的工作。
Unfortunately it might to take hours to do all work.
如何做得更快?
推荐答案
您可以使用 HashSet的
与 ARR2
HashSet<string> arr2Set = new HashSet<string>(arr2);
arr1.Where(x=>arr2Set.Contains(x)).Count();
------------------
|
|->HashSet's contains method executes quickly using hash-based lookup..
不考虑从 ARR2
转换为 arr2Set
,这应该是 O(N)
这篇关于两个字符串数组的快速计数()路口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文