算法:计算两个大量数据的数组交集,差集,并集,(数据差异)


现有两个简单数组 List<string> 分别为 ab

计算两个字符串数组 ab 没有的字符串

立马就写出了Linq算法

List<string> a = new List<string>();
List<string> b = new List<string>();

var data = a.Where(w => !b.Any(t => t == w)).ToList();
GarsonZhang www.yesdotnet.com

数据量不大的时候没问题,如果数据量大的时候效率就很慢很慢,可以测试下 a 和 b 中各有12W条数据的情况,速度简直是惨不忍睹

提高效率的方法

使用Linq扩展

var dta = a.Except(b);
GarsonZhang www.yesdotnet.com

测试发现几乎是秒级别的效率了,固该方法效率很快,

另外还有几个扩展值得记住

string[] arrRate = new string[] { "a", "b", "c", "d" };//A
string[] arrTemp = new string[] { "c", "d", "e" };//B
 
string[] arrUpd = arrRate.Intersect(arrTemp).ToArray();//相同的数据 (结果:c,d)
string[] arrAdd = arrRate.Except(arrTemp).ToArray();//A中有B中没有的 (结果:a,b)
string[] arrNew = arrTemp.Except(arrRate).ToArray();//B中有A中没有的 (结果:e)
GarsonZhang www.yesdotnet.com

进阶学习之原理刨析

为什么Linq的扩展方法效率提升这么多呢?好在Linq是开源的,查看下源代码,源码太长,这里截取几个重要的片段

.net 源代码下载 Download (microsoft.com)

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;

namespace System.Linq
{
    public static partial class Enumerable
    {
        public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
        {
            if (first == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first);
            }

            if (second == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second);
            }

            return ExceptIterator(first, second, null);
        }

        public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
        {
            if (first == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first);
            }

            if (second == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second);
            }

            return ExceptIterator(first, second, comparer);
        }

        private static IEnumerable<TSource> ExceptIterator<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
        {
            Set<TSource> set = new Set<TSource>(comparer);
            set.UnionWith(second);

            foreach (TSource element in first)
            {
                if (set.Add(element))
                {
                    yield return element;
                }
            }
        }
    }
}
GarsonZhang www.yesdotnet.com

 

完整的System.Linq代码参考:

corefx/src/System.Linq/src/System/Linq at master · dotnet/corefx · GitHub

corefx/Except.cs at master · dotnet/corefx · GitHub

 

我们第一次 where 嵌套 any 的写发,重复度为 MN

Linq扩展方法 Except 重复度为 M + N 

大量数据的情况下效率显而易见

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:微信支付:商户API私钥
下一篇:微信支付:header中的mchid与post payload中的mchid不匹配
评论列表

发表评论

评论内容
昵称:
关联文章

算法计算大量数据数组,(数据差异
.net中使用Linq 判断集合是否有集合类
JS合并数组3种方法详解
C#计算日期之间整月
API接口主从表数据(Master-Detail),从表数据种处理方式
WPF 布局 在有限空间内让元素尽可能撑开例子
[WPF] 实现任天堂 Switch 加载动画
TS有对象,便利B对象属性,如果A对象有这个属性,就把B值更新到A对象。
Python数组List检索 三种方法从数组List中检索出符合要求元素
C#和java对URL编码(UrlEncode)差异
iNeuOS工业互联网操作系统,增加算法分析平台,包括快速傅里叶变换、包络分析、倒频谱和自相关等算法
windows平台分布式微服务解决方案(7)--IDataAccess工具介绍(数据访问)
dotnet C# 根据椭圆长度和宽度和旋转角计算出椭圆中心点方法
WPF_15_格式化绑定数据
AgGrid Options列宽度计算
C#计算工龄/年龄
服务器ntlmssp攻击防御措施,windows server大量审核失败问题
在进销存系统中如何基于账期管理进行精准库存计算
东方财富网站接口调用时间戳计算规则
LabVIEW生成.NETDLL——C#下调用NI数据采集设备功能一种方法 [原创www.cnblogs.com/helesheng]

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码