惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 小诈

[.net]正则表达式整理 今日所思 伟大的意大利夺冠了!疯狂庆祝! word add-in 卸载时如何清除自定义的按钮和菜单 制作包含.net framework的安装包 解决不能上网的问题(Wincock绑架) [ASP.NET揭密读书笔记]额外的控件和资源 [ASP.NET揭密读书笔记]应用程序跟踪和监视 [ASP.NET揭密读书笔记]ADO.NET介绍 [ASP.NET揭密读书笔记]用户自定义控件 [ASP.NET揭密读书笔记]连接池 安装Ubuntu 痛苦的胃镜检查 [转载]微软好员工的十个标准 新年新气象,恭喜发财 WEB中服务器端Table的行集中要注意ViewState 2005年年终总结 招聘.NET高级软件工程师 [转贴]Visual Studio 2005常用插件搜罗
DataTable.Select方法的性能问题
小诈 · 2005-12-27 · via 博客园 - 小诈

我们首先看一个例子:
bool a,b,c,d; a=true;b=false;c=false;d=false;
如果我们执行if(a or b or c or d){},我们发现当程序执行到a,发现为true就执行下面的语句了,就没必要去判断b,c,d的值了。

最近在做一些大数据量的多次比较问题,由于多次比较一些记录块,所以将这些记录放到了DataTable中提高查找的性能,使用DataTable.Select(string filterExpression)来查找相关的记录。但是我发现执行的比较慢。然后我把Select方法接受的xpath拆分为几个条件执行,将其中最可能缩小范围的条件传递给filterExpression,然后在找到的集合中过滤其他的条件。发现这样比原来的执行时间少了很多。由此我估计Select算法是运算所有的条件,然后才返回这个记录的。
事不宜迟,我用Reflector.exe查看算法源代码,发现该方法是把这个filterExpression表达式分割成多个表达式,然后遍历记录,计算这些子条件,取到满足条件的记录(算法内部比较复杂,细节上没看完,如果有好的见解欢迎提出),这样就导致了效率的低下。
然后我又做了一个简单的例子验证了一下:

 1DataTable dt = new DataTable();
 2        dt.Columns.Add("a",typeof(int));
 3        dt.Columns.Add("b",typeof(string));
 4        
 5        for(int i=0;i<1000000;i++)
 6        {
 7            DataRow row = dt.NewRow();
 8            row["a"= 0;
 9            row["b"= "2";
10            dt.Rows.Add(row);
11        }

12        long tick = System.DateTime.Now.Ticks;
13        DataRow[] rows = dt.Select("a=0 or (b='1' or b='3' or b='5' or b='6')");
14        System.Console.WriteLine((System.DateTime.Now.Ticks-tick));
15        tick = System.DateTime.Now.Ticks;
16        rows = dt.Select("a=0");
17        System.Console.WriteLine((System.DateTime.Now.Ticks-tick));

在本机测试,发现第一个执行时间是第二个的12倍!

所以在使用该方法时要注意优化自己的查询条件,尤其某个条件能有效的缩小查询范围时应该考虑先用该条件查询,然后对结果再执行条件。