Linq 用得太随意导致的性能问题一则
问题场景
有一个很多条数据的数据库(数据源),在其中找出指定的项,这些项的 ID 位于 给定的列表中,如 TargetList 中。
private readonly IDictionary<string, int> _testSource = new Dictionary<string, int>();
private readonly IList<string> _targetStringList = new List<string>();
直观简洁的 Linq 写法
public long TestFindByLinq()
{
Stopwatch sw = new Stopwatch();
sw.Start();
IList<int> theResultData = _testSource.Where(s => _targetStringList.Contains(s.Key)).Select(s=>s.Value).ToList();
// IList<int> theResultData2 = _targetStringList.Select(f => _testSource[f]).ToList();
sw.Stop();
Console.WriteLine($"[TestFindByLinq] ElapsedTicks:{sw.ElapsedTicks}; TotalTargetCount:{_targetStringList.Count} \n");
return sw.ElapsedTicks;
}
问题在于,如果这么写,将要遍历整个源数据,性能受影响。
看起来麻烦,但性能好很多的写法
public long TestFindByForEach()
{
Stopwatch sw = new Stopwatch();
sw.Start();
IList<int> theResultData = new List<int>();
foreach (string target in _targetStringList)
{
int data = _testSource[target];
theResultData.Add(data);
}
sw.Stop();
Console.WriteLine($"[TestFindByForEach] ElapsedTicks:{sw.ElapsedTicks}; TotalTargetCount:{_targetStringList.Count}\n");
return sw.ElapsedTicks;
}
性能相差多少呢?
从 100000 条数据中,找 6354 条数据。

差不多是 1000 倍以上的性能差距。
有什么启发吗?
Linq 性能不好,有时候可能只是 Linq 写得不好,Linq 写起来很方便,但如果写法中涉及到的查询方式,需要遍历全部数据,性能必然受到影响,如果还要多次遍历全部内容,那就更可怕了。
修正
以上测试考虑问题确实是不全面的,详情可以看评论区大佬的回复。
1 如果知道一定包含,可以用
var testData = _targetStringList.Select(f => _testSource[f]).ToList();
2 可以使用 hashSet
var testData = _testSource.Where(s => _targetStringList.Contains(s.Key)).ToList();
// 在原有的基础上,改动最小可以用HashSet
var hashSet = _targetStringList.ToHashSet();
var testData = _testSource.Where(s => hashSet.Contains(s.Key)).ToList();
// by 评论区 @玩命夜狼
感谢各位大佬指正。
原文链接 : https://www.cnblogs.com/jasongrass/p/10797795.html
附:测试完整源码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace GrassDemoPark.CoreConsoleApp
{
class LinqTest
{
private readonly IDictionary<string, int> _testSource = new Dictionary<string, int>();
private readonly IList<string> _targetStringList = new List<string>();
public LinqTest()
{
for (int i = 0; i < 100000; i++)
{
string guid = Guid.NewGuid().ToString();
_testSource.Add(guid, i);
if (guid.StartsWith("5"))
{
_targetStringList.Add(guid);
}
}
}
public long TestFindByLinq()
{
Stopwatch sw = new Stopwatch();
sw.Start();
IList<int> theResultData = _testSource.Where(s => _targetStringList.Contains(s.Key)).Select(s=>s.Value).ToList();
// IList<int> theResultData2 = _targetStringList.Select(f => _testSource[f]).ToList();
sw.Stop();
Console.WriteLine($"[TestFindByLinq] ElapsedTicks:{sw.ElapsedTicks}; TotalTargetCount:{_targetStringList.Count} \n");
return sw.ElapsedTicks;
}
public long TestFindByForEach()
{
Stopwatch sw = new Stopwatch();
sw.Start();
IList<int> theResultData = new List<int>();
foreach (string target in _targetStringList)
{
int data = _testSource[target];
theResultData.Add(data);
}
sw.Stop();
Console.WriteLine($"[TestFindByForEach] ElapsedTicks:{sw.ElapsedTicks}; TotalTargetCount:{_targetStringList.Count}\n");
return sw.ElapsedTicks;
}
}
}
Linq 用得太随意导致的性能问题一则的更多相关文章
- log4j导致的性能问题
问题背景 双十一零点时,有一个服务A(后文该服务都用A来代替)的tp99由平常的50ms左右突然彪到60000ms,导致调用端积累了几十W的数据,同时,也影响到了同一个docker上的其他服务.那为什 ...
- linux交换区使用过多导致的性能问题
近日,我们开发发现有一台配置相同的服务器跑的特别慢,相同数据量的情况下,其他服务器只要跑10分钟,这台服务器要跑50分钟,经确认,所有的应用层配置参数都相同.上去之后,发现该服务器swap使用比较多, ...
- 分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据
分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据 今天开发找我,说数据库insert不进数据,叫我看一下 他发了一个截图给我 然后我登录上服务器,发现了可疑的地方,而且这个数据库之前 ...
- ssas 为绑定指定的大小太小,导致一个或多个列值被截断
错误信息:ssas 为绑定指定的大小太小,导致一个或多个列值被截断 如果更改了某个维度或是事实表的字段长度,在处理CUBE时提示此错误,我们要做以下更新: 1.刷新数据源视图. 2.打开多维数据集,查 ...
- WebSphere中数据源连接池太小导致的连接超时错误记录
WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...
- Base64实现测试,不要太相信apache-common的性能
针对三种Base64实现: * 自已实现的 * JDK8的java.util.Base64 * apache-common的org.apache.commons.codec.binary.Base64 ...
- EntityFramework Core 3多次Include导致查询性能低之解决方案
前言 上述我们简单讲解了几个小问题,这节我们再来看看如标题EF Core中多次Include导致出现性能的问题,废话少说,直接开门见山. EntityFramework Core 3多次Include ...
- nginx 缓存区太小导致后台Connection reset by peer 报错
问题概述:图片bit 64生成数据流太大,导致小程序分享弹窗的二维码图片生成失败 后台报错: 排查: Client------>nginx------->h5------>nginx ...
- sqlt 之 分析 DB upgrade 导致SQL 性能下降 的方法 xplore
https://blog.csdn.net/lukeUnique/article/details/79331779 https://mauro-pagano.com/2014/10/27/when-t ...
随机推荐
- vsftpd服务
vsftpd服务 文件传输协议(file transfer protocol,FTP),基于该协议FTP客户端与服务端可以实现共享文件,上传文件,下载文件.ftp基于TCP协议生成一个虚拟的连接,主要 ...
- js闭包和原型链好文
http://www.cnblogs.com/wangfupeng1988/p/3977924.html
- c# 第二节 c#的常用IDE环境
本节内容: 1:常用ide环境 2:Visual Studio 简介 3:Visual Studio Express简介 4:Sharp Develop 5: IDE 与 .Net的版本 1:常用i ...
- 201871010136-赵艳强《面向对象程序设计(java)》第十四周学习总结
201871010136-赵艳强<面向对象程序设计(java)>第十四周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...
- myeclipse开发javaweb难点总结
修改项目部署名:右键项目首选项—搜web 创建servlet:先在src下面建包,com.yhh.servlet,然后建新servlet,第二页改url为别名,这种方法xml会自动配置好 配置哪个服务 ...
- tecplot无法处理高版本fluent导出的Ensight格式
高版本的Fluent完成计算,将计算结果导出为Ensight格式,然后再导入tecplot当中进行后处理的时候会遇见如下的错误: 但是将低版本的Fluent计算结果导出为Ensight格式,却可以顺利 ...
- centos7.5安装java JDK、tomcat、mysql
参考资料: https://www.cnblogs.com/sxdcgaq8080/p/7492426.html https://blog.csdn.net/ds986619036/article/d ...
- STM32Cube在Main里判断USB是否已连接到电脑
首先添加这两个Includes: #include "usbd_def.h" #include "usbd_hid.h" 然后就可以在代码里用这个来判断是否有连 ...
- pgsql_pg的数据类型
PostgreSQL 提供了丰富的数据类型.用户可以使用 CREATE TYPE 命令在数据库中创建新的数据类型.PostgreSQL 的数据类型被分为四种,分别是基本数据类型.复合数据类型.域和伪类 ...
- Nacos配置的多环境管理
实现多环境管理有下面几种方式 1.使用Data ID与profiles实现 在 Nacos Config Starter 中,dataId 的拼接格式为 ${prefix} - ${spring.pr ...