最快比较两个列表与LT方式;&GT; [英] Quickest way to compare two List<>
问题描述
什么是最快的(至少和资源密集型)来比较两个巨大的(> 50.000项目),结果有两个列表类似下面的:
What is the quickest (and least resource intensive) to compare two massive (>50.000 items) and as a result have two lists like the ones below:
- 项目,在第一个列表中,但不会在第二个 显示
- 项目,在第二个列表,但不是在第一个 显示
目前我的列表或IReadOnlyCollection工作,并在LINQ查询解决此问题:
Currently I'm working with the List or IReadOnlyCollection and solve this issue in a linq query:
var list1 = list.Where(i => !list2.Contains(i)).ToList();
var list2 = list2.Where(i => !list.Contains(i)).ToList();
但是,这并不执行好,因为我想。
任何想法,这使得更快,资源较少,因为我需要处理大量的名单中?
But this doesn't perform as good as i would like. Any idea of making this quicker and less resource intensive as i need to process a lot of lists?
推荐答案
使用 除了
:
var firstNotSecond = list1.Except(list2).ToList();
var secondNotFirst = list2.Except(list1).ToList();
我怀疑有这实际上是略高于这个速度的方法,但即使这将是的极大地的比你的O(N * M)的方法要快。
I suspect there are approaches which would actually be marginally faster than this, but even this will be vastly faster than your O(N * M) approach.
这篇关于最快比较两个列表与LT方式;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!