维生素C.net
Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。
难忘的1654天
博客园  首页  新随笔  新文章  联系  管理  订阅 订阅
随笔- 220  文章- 1  评论- 1648 
为LINQ提速的i4o和增强功能的SLINQ

i4o是对LINQ的一个扩展,通过允许我们在对象上添加“索引”来提高LINQ运算速度,作者号称使用i4o后速度提升often over one thousand times。

我们在进行数据库查询优化时,往往第一想到的就是给Tables添加合适的Index来大幅度提升执行效率,i4o的实现也是类似这个方式,我们只要给class添加一个Indexable属性,然后使用IndexableCollection<T>来实现一个使用“索引”的类的集合就可以了,这样比起顺序性的搜索就在一定程度上提高了速度。

比如我们可以这样用:

1var customers = new IndexableCollection<CnblogUser>() { 
2                new Customer {Key = 1, Name = "fanweixiao" },
3                new Customer {Key = 2, Name = "lovewangshu" }
4}
;

i4o中对Where的扩展

 1//extend the where when we are working with indexable collections! 
 2        public static IEnumerable<T> Where<T>
 3        (
 4            this IndexableCollection<T> sourceCollection,
 5            Expression<Func<T, bool>> expr
 6            )
 7        {
 8            //our indexes work from the hash values of that which is indexed, regardless of type
 9            int? hashRight = null;
10            bool noIndex = true;
11            //indexes only work on equality expressions here
12            if (expr.Body.NodeType == ExpressionType.Equal)
13            {
14                //Equality is a binary expression
15                BinaryExpression binExp = (BinaryExpression)expr.Body;
16                //Get some aliases for either side
17                Expression leftSide = binExp.Left;
18                Expression rightSide = binExp.Right;
19
20                hashRight = GetHashRight(leftSide, rightSide);
21
22                //if we were able to create a hash from the right side (likely)
23                if (hashRight.HasValue && HasIndexablePropertyOnLeft<T>(leftSide,sourceCollection))
24                {
25                    //cast to MemberExpression - it allows us to get the property
26                    MemberExpression propExp = (MemberExpression)leftSide;
27                    string property = propExp.Member.Name;
28                    Dictionary<int, List<T>> myIndex =
29                            sourceCollection.GetIndexByProperty(property);
30                    if (myIndex.ContainsKey(hashRight.Value))
31                    {
32                        IEnumerable<T> sourceEnum = myIndex[hashRight.Value].AsEnumerable<T>();
33                        IEnumerable<T> result = sourceEnum.Where<T>(expr.Compile());
34                        foreach (T item in result)
35                            yield return item;
36                    }

37                    noIndex = false; //we found an index, whether it had values or not is another matter
38                }

39
40            }

41            if (noIndex) //no index?  just do it the normal slow way then
42            {
43                IEnumerable<T> sourceEnum = sourceCollection.AsEnumerable<T>();
44                IEnumerable<T> result = sourceEnum.Where<T>(expr.Compile());
45                foreach (T resultItem in result)
46                    yield return resultItem;
47            }

48
49        }



而
SLINQ则是可以让LINQ作用于streaming data上的,目前这个项目只算是个Demo版本,实现方式是为LINQ添加了一系列的扩展方法,有兴趣的朋友可以down下sourcecode来看看,需要注意的是要安装Visual Studio Orcas beta 1。

顺便帖两个codeplex上与LINQ相关的项目:

  1. LINQ是怎么来的?看LINQ-SQO
  2. 在C++/CLI上用LINQ:LINQ Extensions
posted on 2007-06-28 01:40 维生素C.NET 阅读(2350) 评论(7)  编辑 收藏 所属分类: ASP.NET

发表评论
  回复  引用  查看    
2007-06-28 08:33 | 补丁      
缓存?
  回复  引用  查看    
2007-06-28 09:27 | Anders Liu      
果然是用了Dictonary,这个玩艺按键搜索时间复杂度竟然是O(1),我们都爱用它~
  回复  引用  查看    
2007-06-28 10:42 | watson hua      
楼住的index对于可序集的一维随机查找确实是有帮助,也算是一劳永逸的事。
但貌似没有什么新意,而且我认为,把数据交给算法要比把算法交给数据的场合要多得多。
个人观点,欢迎讨论。
  回复  引用  查看    
2007-06-28 13:08 | 维生素C.NET      
@watson hua
我认为i4o是针对LINQ在某种场合下发挥的功能的优化。而LINQ设计的目的并不仅仅是提供一组操作符方便处理数据(算法)而是一种处理问题的方案(场合)。
  回复  引用  查看    
2007-06-28 18:08 | 直心眼      
没用过LINQ,问一下这种弱类型.会对性能产生多少影响?有没有相关性能比较文章
  回复  引用  查看    
2007-07-05 22:41 | Boler Guo      
谁说Linq是弱类型了?强的很~
  回复  引用  查看    
2008-02-10 00:45 | fox23      
--引用--------------------------------------------------
直心眼: 没用过LINQ,问一下这种弱类型.会对性能产生多少影响?有没有相关性能比较文章
--------------------------------------------------------
性能上,MS一定是做了优化的。Linq可以让各位MIS开发者能轻易的在C#中做查询,而且编译时就能察觉错误(当然它的功能不只如此)
新用户注册  刷新评论列表  

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
网站首页

新闻频道

社区

小组

博问

网摘

闪存

找找看

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-06-28 01:50 编辑过

相关文章:
ASP.NET实用技巧(一)
ASP.NET跨页面传值技巧总结
关于IE问题,请教和求救
Windows 2000 常见进程功能描述[转帖]
ASP.NET下如何浏览客户端文件夹
学校有关于ASP.NET和JSP的课程 有必要都学习吗?
建议博问中增加ASP.NET MVC分类
深入剖析ASP.NET组件设计

相关链接:

所属分类的其他文章:
为LINQ提速的i4o和增强功能的SLINQ
结构体,值类型和接口
ASP.NET崩溃 - SiteMap里的疯狂循环(概译)
Inbox.com使用的asp.net和ajax技术
NOCOUNT如何影响ADO.NET(SET NOCOUNT ON的性能问题)
30hrs Wire [1]
利用数学方法来大大降低一个逻辑判断实现的难度的例子
在配置使用Membership或其他的Providers的ASP.NET2.0时一定要设置applicationName属性
在Atlas中实现检测postback progress的状态的方法
【翻译】Atlas Documents : UpdatePanel Class

最新IT新闻:
51.COM技术副总裁邵辉跳槽百度
Mono 2.0终于到来
百度任命李一男担任首席技术官
Google Knol 开始尝试有声版
12日电脑与人进行世纪对话 可思考机器或诞生
 



公告

view my mvp profile 看看有多少人来访问我的Blog了!
hotmail

<2008年2月>
日一二三四五六
272829303112
3456789
10111213141516
17181920212223
2425262728291
2345678

与我联系

  • 发短消息

搜索

 

常用链接

  • 我的随笔
  • 我的空间
  • 我的短信
  • 我的评论
  • 更多链接
  • 我的参与
  • 我的新闻
  • 最新评论
  • 我的标签

留言簿(168)

  • 给我留言
  • 查看留言

我参与的团队

  • 北京.NET俱乐部(4/1544)
  • 烟台.NET俱乐部(0/47)
  • ASP.NET AJAX (Atlas)学习(0/1352)
  • MVP(微软最有价值专家)团队(1/633)
  • 博客园培训团队(0/111)
  • Silverlight学习与研究(0/285)
  • CLR基础研究团队(1/405)

随笔分类(148)

  • ASP.NET(26)
  • Code Warehouse(20)
  • IronRuby,DLR(2)
  • LINQ(3)
  • Reading(3)
  • Training@cnblogs(23)
  • Ubuntu(4)
  • Windows Live(6)
  • Windows Mobile(7)
  • XHTML & Web Standard(54)

随笔档案(220)

  • 2008年3月 (2)
  • 2008年1月 (3)
  • 2007年12月 (3)
  • 2007年9月 (1)
  • 2007年8月 (2)
  • 2007年7月 (3)
  • 2007年6月 (3)
  • 2007年3月 (4)
  • 2007年2月 (3)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (8)
  • 2006年10月 (6)
  • 2006年9月 (11)
  • 2006年8月 (5)
  • 2006年7月 (4)
  • 2006年6月 (1)
  • 2006年5月 (10)
  • 2006年4月 (8)
  • 2006年2月 (2)
  • 2006年1月 (1)
  • 2005年12月 (11)
  • 2005年11月 (13)
  • 2005年10月 (3)
  • 2005年9月 (1)
  • 2005年8月 (4)
  • 2005年7月 (3)
  • 2005年6月 (4)
  • 2005年4月 (5)
  • 2005年3月 (10)
  • 2005年2月 (7)
  • 2005年1月 (28)
  • 2004年12月 (15)
  • 2004年11月 (10)
  • 2004年10月 (5)
  • 2004年9月 (1)
  • 2004年6月 (13)
  • 2004年5月 (5)

文章档案(1)

  • 2005年5月 (1)

相册

  • ASPNET2tutorial
  • BlogUsing
  • My love and my friends
  • newGallery
  • 下一代网络图片

.net网站收藏

  • ASP.NET2.0 Tutorial
  • CodeBetter.com
  • F#
  • IIS.net
  • MS NewsGroup
  • NewsGroups
  • OnlyVC.org
  • VWD2005GuidedTour
  • ZDNet China软件技术专区

OSS 2007

  • Charsh
  • Kaneboy
  • Official Team Blog

Python

  • BeginnersGuide

好友的BLOG

  • DemoFox@JoyCode
  • DflyingChen
  • dudu
  • EricLee
  • hbifts
  • idior
  • Jesee Qing
  • Lion
  • Rickie
  • Samuel
  • Steph`s Website
  • 翱翔.Net
  • 陈敬熙
  • 发条木偶
  • 葛涵涛
  • 古道风
  • 寒枫天伤
  • 老猫の理想
  • 刘老师
  • 刘彦博
  • 吕震宇
  • 木野狐
  • 佘广
  • 王sir
  • 小涛
  • 小新
  • 肖老师
  • 旋哥

积分与排名

  • 积分 - 397788
  • 排名 - 56

最新评论

  • 1. re: 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ]
  • 加! 加! 加!
  • --… 黒液...
  • 2. re: 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ]
  • 在下也是开发java Web的 希望加入!!
  • --… 黒液...
  • 3. re: .NET Beginner Training Step by Step开始启动
  • 我的ID: nocry

    申请参加 谢谢
  • --夕阳
  • 4. re: .NET Beginner Training Step by Step开始启动
  • 早班加入
  • --石牌村夫
  • 5. re: .NET Beginner Training Step by Step开始启动
  • 申请加入!谢谢!
  • --童话@混子

阅读排行榜

  • 1. 英文名字及含义(25339)
  • 2. SQL Server 2005 Remote Access(15130)
  • 3. Visual Studio 2005 Team Edition和SQL Server 2005的下载(14358)
  • 4. Windows Installer 3.1(11513)
  • 5. Visual Studio 2005 Professional Released(10991)

评论排行榜

  • 1. .NET Beginner Training Step by Step开始启动(320)
  • 2. Windows Live Messenger 8.0 Beta 的邀请(100)
  • 3. 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ](90)
  • 4. 博客园新手.net技术培训活动(55)
  • 5. 为什么在vista上做开发?(54)
Copyright ©2008 维生素C.NET