数据清洗使用Parallel 多线程

一.概述

  在开发数据清洗时,ES数据集有600w条,每一条的子对象又有几十条,需要拿到子对象去重后的集合,使用分批提取ES数据,共535批。开始使用List来操作,关键代码如下:

           var specListAll = new List<SpecInfo>();
            for (int i = 0; i < batchCount; i++)
            {
                //从es提取一批数据
                //每条数据提取子集合到list
               //下面去重后添加到新集合中
                foreach (var specDesc in list)
                {
                    if (specListAll.Count(w => w.NameJoinValue == specDesc.NameJoinValue) == 0)
                        specListAll.Add(specDesc);
                }
            }

   使用计时器,第一批数据执行完耗时3分29秒 ,去重后进入15542个到specListAll集合中,  535批预估共执行31.2小时。

   下面使用Parallel 多线程来实现去重后,添加到新集合中,关键代码如下:

           var specListAll = new ConcurrentBag<SpecInfo>();
            for (int i = 0; i < batchCount; i++)
            {
                //从es提取一批数据
                //每条数据提取子集合到list
                //下面去重后添加到新集合中
                Parallel.ForEach(list, specDesc =>
                {
                    if (specListAll.Count(w => w.NameJoinValue == specDesc.NameJoinValue) == 0)
                        specListAll.Add(specDesc);
                });
            }

   使用计时器,第一批数据执行完耗时2分19秒 ,去重后进入15542个到specListAll集合中,  535批预估共执行20.8小时。

   最后查看CPU,使用Parallel 多线程会高出30%的使用率

二.改进

    在清洗中,发现使用specListAll.Count来去重复很耗时间,改进后,只需要2个多小时清洗完成,代码如下:

    var specListAll = new ConcurrentBag<SpecInfo>();
            for (int i = 0; i < batchCount; i++)
            {
                //从es提取一批数据
                //每条数据提取子集合到list
                //下面去重后添加到新集合中
                Parallel.ForEach(list, specDesc =>
                {
                    //if (specListAll.Count(w => w.NameJoinValue == specDesc.NameJoinValue) == 0)
                        specListAll.Add(specDesc);
                });
            }
   //最后加上去重
   var specListAll2=specListAll.Distinct().ToList();
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值