【发布时间】:2020-02-08 01:07:05
【问题描述】:
我有两个庞大的字典,一个名为 DictHashesSource 有 2256001 行,另一个字典名为 DictHashesTarget 有 2061735 行。
Dictionary<int, string> DictHashesSource = new Dictionary<int, string>();
Dictionary<int, string> DictHashesTarget = new Dictionary<int, string>();
我想要做的是,对于 DictHashesSource 的每个元素,检索 DictHashesTarget 中匹配的所有元素,并以相反的方式执行完全相同的操作。 为此,我使用了如下所示的 LINQ:
IEnumerable<string> interceptedRowsSource = DictHashesSource.Values.Where(x => DictHashesTarget.Values.Contains(x)).ToList();
IEnumerable<string> interceptedRowsTarget = DictHashesTarget.Values.Where(x => DictHashesSource.Values.Contains(x)).ToList();
问题是,由于两个字典真的很大,每次操作都需要1个多小时,有什么办法可以降低这个算法的复杂度?
注意:我真的必须使用两个字典,因为我将不得不在进一步的操作中使用这些键。
另一个注意事项:相同的值在两个字典中没有相同的键
【问题讨论】:
-
请提供更多信息。 DictHashesSource 定义为什么? DictHashesTarget 定义为什么?是否需要在其他操作之前实现它(.ToList())?
-
每次调用
Values的时间复杂度为 O(1),因此使用Contains的每个语句都是 O(n2*2) -
dict1.Values.Intersect(dict2.Values);? -
字典不适用于这个,你需要像这样的树型搜索:github.com/gmamaladze/trienet
-
字典是如何填充的?