如何使用IEqualityComparer加快许多字段的比较?

我在数据库中有一个项目列表,我用以下方式检索它:AE_AlignedPartners


List<AE_AlignedPartners> ae_alignedPartners_olds = ctx.AE_AlignedPartners.AsNoTracking().ToList();

比,我得到并序列化了一个使用JSON的新列表(具有相同的对象类型):


List<AE_AlignedPartners> ae_alignedPartners_news = GetJSONPartnersList();

比我得到两者的交集:


var IDSIntersections = (from itemNew in ae_alignedPartners_news

                        join itemOld in ae_alignedPartners_olds on itemNew.ObjectID equals itemOld.ObjectID

                        select itemNew).Select(p => p.ObjectID).ToList();

现在,由于这些交集,我需要检查某些记录是否已更改,检查许多字段,然后添加到更新的“监视”列表中。代码如下:


IList<AE_AlignedPartners> ae_alignedPartners_toUpdate = new List<AE_AlignedPartners>();

foreach (var item in IDSIntersections)

{

    var itemOld = ae_alignedPartners_olds.First(p => p.ObjectID == item);

    var itemNew = ae_alignedPartners_news.First(p => p.ObjectID == item);


    if (itemOld.Field1 != itemNew.Field1 ||

        itemOld.Field2 != itemNew.Field2 ||

        itemOld.Field3 != itemNew.Field3 ||

        itemOld.Field4 != itemNew.Field4 ||

        itemOld.Field5 != itemNew.Field5 ||

        itemOld.Field6 != itemNew.Field6 ||

        itemOld.Field7 != itemNew.Field7 ||

        itemOld.Field8 != itemNew.Field8 ||

        itemOld.Field9 != itemNew.Field9)

    {

        AE_AlignedPartners toUpdate = mapper.Map<AE_AlignedPartners, AE_AlignedPartners>(itemNew);

        toUpdate.ID = itemOld.ID;


        ae_alignedPartners_toUpdate.Add(toUpdate);

    }

}

这是非常慢的(发布约4分钟,约70k条记录)。


最近我在这里发现了,它确实加快了比较的进程。IEqualityComparer


在这种情况下,我可以利用它吗?或者哪些是有效的优化?


我不会使用建议的,因为这将意味着现在需要大量的重构(我将在下一个项目中这样做,承诺)。FullOuterJoin


有什么提示吗?谢谢


阿晨1998
浏览 118回答 2
2回答

千万里不及你

您有嵌套循环实现// O(N) : Loop over IDSIntersectionsforeach (var item in IDSIntersections){&nbsp; &nbsp; // O(N) : Again, loop over ae_alignedPartners_olds&nbsp; &nbsp; var itemOld = ae_alignedPartners_olds.First(p => p.ObjectID == item);&nbsp; &nbsp; var itemNew = ae_alignedPartners_news.First(p => p.ObjectID == item);&nbsp; &nbsp; ...在最坏的情况下,您将具有时间复杂性;数十亿个循环: .让我们借助字典摆脱内部循环:O(N) * O(N) = O(N**2)70k * 70k ~ 5e9// O(N)var dictOld = ae_alignedPartners_olds&nbsp; .GroupBy(p => p.ObjectID) // ObjectID should be a int, string or provide good GetHashCode()&nbsp; .ToDictionary(chunk => chunk.Key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunk => chunk.First());// O(N)var dictNew = ae_alignedPartners_news&nbsp; .GroupBy(p => p.ObjectID)&nbsp;&nbsp; .ToDictionary(chunk => chunk.Key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunk => chunk.First());// O(N)foreach (var item in IDSIntersections){&nbsp; &nbsp;// O(1) : no loops when finding value by key in dictionary&nbsp; &nbsp;var itemOld = dictOld[item];&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;var itemNew = dictNew[item];&nbsp; &nbsp;...&nbsp;现在我们将有关于循环:3 * O(N)3 * 70k ~ 2e5

慕哥9229398

自定义会很好,但不是因为它提高了性能,它需要做同样的比较。但是因为在那里封装逻辑使其更易于维护,可读和可重用。您可以将其用于许多 LINQ 方法。IEqualityComparer<AE_AlignedPartners>缓慢的是,您总是在 -循环中搜索旧项和新项。ObjectIdforeach您不需要选择两者的共同点,如果您已经加入了旧的和新的,只需将整个实例存储在匿名类型中:ObjectIDvar intersections = from itemNew in ae_alignedPartners_news&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join itemOld in ae_alignedPartners_olds on itemNew.ObjectID equals itemOld.ObjectID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new { New = itemNew, Old = itemOld };foreach(var x in intersections){&nbsp; &nbsp; var itemOld = x.Old;&nbsp; &nbsp; var itemNew = x.New;&nbsp; &nbsp; // ...&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP