c# - 比较大列表
问题描述
我有两个非常大的列表,每个列表有几十万个项目,一个是完整的,另一个是缺少项目。我需要知道不完整列表中缺少哪些项目。我已经尝试过使用Enumerable.Except
,但需要很长时间才能完全比较它们。
解决方案
根据您提供的信息,我认为您应该能够通过在比较之前将字符串转换为整数类型来获得良好的性能优势。
我已经编写了实现的 LINQ 和非 LINQ 版本。主要区别在于.ToDictionary
,由于重新分配了更大的内存插槽,调用会稍微慢一些。在非LINQ版本中我们可以使用a HashSet
,但是我使用的版本( 4.6.1
)不允许我通过指定容量来构造。
// Sample String POS0001:615155172
static long GetKey(string s) => long.Parse("1" + s.Substring(3, 4) + s.Substring(8));
static IEnumerable<string> FindMissing(IEnumerable<string> masterList, ICollection<string> missingList) {
var missingSet = new Dictionary<long, bool>(missingList.Count);
foreach (string s in missingList)
missingSet.Add(GetKey(s), true);
// Compact LINQ Way, but potentially, ineffecient
//var missingSet = missingList.ToDictionary(GetKey, s => true);
return masterList.Where(s => !missingSet.ContainsKey(GetKey(s)));
}
由于您的数据已经排序,因此有更多涉及的单程方法来解决您的问题。让我知道这是否适合您,因为我没有测试台来测试它。
推荐阅读
- c++ - Qt how to connecto Line Edit to progress bar
- c++ - Execution order of operator new and argument of constructor
- python - NameError:“线性回归”未定义
- r - R - 将变量传递给ddply,按名称或索引引用列,快速语法问题
- php - 如何创建动态函数并调用该静态函数
- c# - C# 更新 ListView 项目而不单击 listview 项目行
- android - PlayConsole 上我的 Flutter 应用支持的设备很少
- python - 为什么 xroot.iter() 可以打印 XPath,但不能作为 Python 中的 XPath 列表返回?
- php - 我的会话在登录实时服务器后自动过期?
- clojure - 如何在 -main 而不是退出后继续使用 repl 运行?