LeM*_*sel 6 c# optimization array-intersect
我需要计算对应于两个大字符串数组的交集的元素数量,并且非常快.
我使用以下代码:
arr1[i].Intersect(arr2[j]).Count()
Run Code Online (Sandbox Code Playgroud)
对于CPU时间,VS Profiler指示
System.Linq.Enumerable.Count()System.Linq.Enumerable.Intersect()不幸的是,完成所有工作可能需要数小时.
怎么做得更快?
您可以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..
Run Code Online (Sandbox Code Playgroud)
arr2不考虑从到 的转换arr2Set,这应该是O(n)
| 归档时间: |
|
| 查看次数: |
3176 次 |
| 最近记录: |